In C#,
Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it.
- Math.Abs(Decimal)
- Math.Abs(Double)
- Math.Abs(Int16)
- Math.Abs(Int32)
- Math.Abs(Int64)
- Math.Abs(SByte)
- Math.Abs(Single)
Math.Abs(Decimal)
This method is used to return the absolute value of a Decimal number.
Syntax:
public static decimal Abs (decimal val);
Parameter:
val: It is the required number which is greater than or equal to Decimal.MinValue, but less than or equal to Decimal.MaxValue of type System.Decimal.
Return Type: It returns a decimal number say r, such that 0 ≤ r ≤ Decimal.MaxValue.
Example:
Output:
Absolute value of -79228162514264337593543950335 = 79228162514264337593543950335
Absolute value of 45.14 = 45.14
Absolute value of 0 = 0
Absolute value of -17.47 = 17.47
Absolute value of 79228162514264337593543950335 = 79228162514264337593543950335
Math.Abs(Double)
This method is used to return the absolute value of a double-precision floating-point number.
Syntax:
public static double Abs (double val);
Parameter:
val: It is the required number which is greater than or equal to Double.MinValue, but less than or equal to Double.MaxValue of type System.Double.
Return Type: It returns a double-precision floating-point number say r, such that 0 ≤ r ≤ Double.MaxValue.
Note:
- If val is equal to NegativeInfinity or PositiveInfinity, the return value will be PositiveInfinity.
- If the val is equal to NaN then return value will be NaN.
Example:
Output:
Absolute value of -1.79769313486232E+308 = 1.79769313486232E+308
Absolute value of 27.58 = 27.58
Absolute value of 0 = 0
Absolute value of 564800000000 = 564800000000
Absolute value of NaN = NaN
Absolute value of 1.79769313486232E+308 = 1.79769313486232E+308
Math.Abs(Int16)
This method is used to return the absolute value of a 16-bit signed integer.
Syntax:
public static short Abs (short val);
Parameter:
val: It is the required number which is greater than Int16.MinValue, but less than or equal to Int16.MaxValue of type System.Int16.
Return Type: It returns 16-bit signed integer say r, such that 0 ≤ r ≤ Int16.MaxValue.
Exception: This method will give OverflowException if the value of val is equals to Int16.MinValue.
Example:
Output:
Absolute value of 32767 = 32767
Absolute value of 1482 = 1482
Absolute value of -142 = 142
Absolute value of 0 = 0
Math.Abs(Int32)
This method is used to return the absolute value of a 32-bit signed integer.
Syntax:
public static int Abs (int val);
Parameter:
val: It is the required number which is greater than Int32.MinValue, but less than or equal to Int32.MaxValue of type System.Int32.
Return Type: It returns 32-bit signed integer say r, such that 0 ≤ r ≤ Int32.MaxValue.
Exception: This method will give OverflowException if the value of val is equals to Int32.MinValue.
Example: