What is Math.Abs() in C#?
C# has a built-in Math class that provides useful mathematical functions and operations. The class has the Abs() function, which is used to compute the absolute value of a specified number.
Syntax
Abs (type param);
where:
paramis a number greater than or equal toMinValueand less than or equal toMaxValue.
Return value
Returns a value such that:
- 0 <= value <=
MaxValue
MinValueandMaxValueare equal to the minimum and maximum value of integer that can be stored in that specific type.
Variants
Abs(Decimal)
This function returns the absolute value of a decimal number.
public static decimal Abs (decimal param);
Abs(Double)
This function returns the absolute value of a double-precision floating-point number.
public static double Abs (double param);
Abs(Int16)
This function returns the absolute value of a 16-bit signed number.
public static short Abs (short value);
Abs(Int32)
This function returns the absolute value of a 32-bit signed number.
public static int Abs (int param);
Abs(Int64)
This function returns the absolute value of a 64-bit signed number.
public static long Abs (long param);
Abs(SByte)
This function returns the absolute value of an 8-bit signed number.
public static sbyte Abs (sbyte param);
Abs(Single)
This function returns the absolute value of a single-precision floating-point number.
public static float Abs (float param);
Code
using System;class HelloWorld{static void Main(){System.Console.WriteLine(Math.Abs(-2));System.Console.WriteLine(Math.Abs(-58.2M));System.Console.WriteLine(Math.Abs(-58.2));System.Console.WriteLine(Math.Abs(0.0));}}