.Abs()
Published Apr 11, 2023Updated May 15, 2024
Contribute to Docs
The Math.Abs()
class method returns the absolute value of a given number.
Syntax
Math.Abs(number);
The Math.Abs()
method takes only one parameter, number
, a decimal
, double
or integer
type number. The method returns the absolute value of the number
with the same type as the number
, except if the value of number
equals:
NaN
(not a number), then it returnsNaN
NegativeInfinity
, then it returnsPositiveInfinity
PositiveInfinity
, then it also returnsPositiveInfinity
Example
The following example uses the Math.Abs()
method to return the absolute value of a decimal
and a double
type number. Then, the Console.WriteLine()
function prints the results to the console:
using System;public class Example {public static void Main() {decimal num1 = -1.23M;double num2 = 6.674E-11;decimal abs1 = Math.Abs(num1);double abs2 = Math.Abs(num2);Console.WriteLine("The absolute value of " + num1 + " is: " + abs1);Console.WriteLine("The absolute value of " + num2 + " is: " + abs2);}}
The example will result in the following output:
The absolute value of -1.23 is: 1.23The absolute value of 6.674E-11 is: 6.674E-11
Codebyte Example
The following example is runnable and returns the absolute value of a double
type number:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C#
Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.Beginner Friendly23 hours