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.
Abs (type param);
where:
param
is a number greater than or equal to MinValue
and less than or equal to MaxValue
.Returns a value such that:
MaxValue
MinValue
andMaxValue
are equal to the minimum and maximum value of integer that can be stored in that specific type.
This function returns the absolute value of a decimal number.
public static decimal Abs (decimal param);
This function returns the absolute value of a double-precision floating-point number.
public static double Abs (double param);
This function returns the absolute value of a 16-bit signed number.
public static short Abs (short value);
This function returns the absolute value of a 32-bit signed number.
public static int Abs (int param);
This function returns the absolute value of a 64-bit signed number.
public static long Abs (long param);
This function returns the absolute value of an 8-bit signed number.
public static sbyte Abs (sbyte param);
This function returns the absolute value of a single-precision floating-point number.
public static float Abs (float param);
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));}}