Edit

Share via


Math.Abs Method

Definition

Returns the absolute value of a specified number.

Overloads

Abs(Decimal)

Returns the absolute value of a Decimal number.

Abs(Double)

Returns the absolute value of a double-precision floating-point number.

Abs(Int16)

Returns the absolute value of a 16-bit signed integer.

Abs(Int32)

Returns the absolute value of a 32-bit signed integer.

Abs(Int64)

Returns the absolute value of a 64-bit signed integer.

Abs(IntPtr)

Returns the absolute value of a native signed integer.

Abs(SByte)

Returns the absolute value of an 8-bit signed integer.

Abs(Single)

Returns the absolute value of a single-precision floating-point number.

Abs(Decimal)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the absolute value of a Decimal number.

C#
public static decimal Abs(decimal value);

Parameters

value
Decimal

A number that is greater than or equal to Decimal.MinValue, but less than or equal to Decimal.MaxValue.

Returns

A decimal number, x, such that 0 ≤ x ≤ Decimal.MaxValue.

Examples

The following example uses the Abs(Decimal) method to get the absolute value of a number of Decimal values.

C#
decimal[] decimals = { Decimal.MaxValue, 12.45M, 0M, -19.69M,
                       Decimal.MinValue };
foreach (decimal value in decimals)
   Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");

// The example displays the following output:
//       Abs(79228162514264337593543950335) = 79228162514264337593543950335
//       Abs(12.45) = 12.45
//       Abs(0) = 0
//       Abs(-19.69) = 19.69
//       Abs(-79228162514264337593543950335) = 79228162514264337593543950335

Remarks

The absolute value of a Decimal is its numeric value without its sign. For example, the absolute value of both 1.2 and -1.2 is 1.2.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Abs(Double)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the absolute value of a double-precision floating-point number.

C#
public static double Abs(double value);

Parameters

value
Double

A number that is greater than or equal to Double.MinValue, but less than or equal to Double.MaxValue.

Returns

A double-precision floating-point number, x, such that 0 ≤ x ≤ Double.MaxValue.

Examples

The following example uses the Abs(Double) method to get the absolute value of a number of Double values.

C#
double[] doubles = { Double.MaxValue, 16.354e-17, 15.098123, 0,
                     -19.069713, -15.058e18, Double.MinValue };
foreach (double value in doubles)
   Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");

// The example displays the following output:
//       Abs(1.79769313486232E+308) = 1.79769313486232E+308
//       Abs(1.6354E-16) = 1.6354E-16
//       Abs(15.098123) = 15.098123
//       Abs(0) = 0
//       Abs(-19.069713) = 19.069713
//       Abs(-1.5058E+19) = 1.5058E+19
//       Abs(-1.79769313486232E+308) = 1.79769313486232E+308

Remarks

The absolute value of a Double is its numeric value without its sign. For example, the absolute value of both 1.2e03 and -1.2e03 is 1.2e03.

If value is equal to NegativeInfinity or PositiveInfinity, the return value is PositiveInfinity. If value is equal to NaN, the return value is NaN.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Abs(Int16)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the absolute value of a 16-bit signed integer.

C#
public static short Abs(short value);

Parameters

value
Int16

A number that is greater than Int16.MinValue, but less than or equal to Int16.MaxValue.

Returns

A 16-bit signed integer, x, such that 0 ≤ x ≤ Int16.MaxValue.

Exceptions

Examples

The following example uses the Abs(Int16) method to get the absolute value of a number of Int16 values.

C#
short[] values = { Int16.MaxValue, 10328, 0, -1476, Int16.MinValue };
foreach (short value in values)
{
   try {
      Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
   }
   catch (OverflowException) {
      Console.WriteLine("Unable to calculate the absolute value of {0}.",
                        value);
   }
}

// The example displays the following output:
//       Abs(32767) = 32767
//       Abs(10328) = 10328
//       Abs(0) = 0
//       Abs(-1476) = 1476
//       Unable to calculate the absolute value of -32768.

Remarks

The absolute value of an Int16 is its numeric value without its sign. For example, the absolute value of both 123 and -123 is 123.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Abs(Int32)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the absolute value of a 32-bit signed integer.

C#
public static int Abs(int value);

Parameters

value
Int32

A number that is greater than Int32.MinValue, but less than or equal to Int32.MaxValue.

Returns

A 32-bit signed integer, x, such that 0 ≤ x ≤ Int32.MaxValue.

Exceptions

Examples

The following example uses the Abs(Int32) method to get the absolute value of a number of Int32 values.

C#
int[] values = { Int32.MaxValue, 16921, 0, -804128, Int32.MinValue };
foreach (int value in values)
{
   try {
      Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
   }
   catch (OverflowException) {
      Console.WriteLine("Unable to calculate the absolute value of {0}.",
                        value);
   }
}

// The example displays the following output:
//       Abs(2147483647) = 2147483647
//       Abs(16921) = 16921
//       Abs(0) = 0
//       Abs(-804128) = 804128
//       Unable to calculate the absolute value of -2147483648.

Remarks

The absolute value of an Int32 is its numeric value without its sign. For example, the absolute value of both 123 and -123 is 123.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Abs(Int64)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the absolute value of a 64-bit signed integer.

C#
public static long Abs(long value);

Parameters

value
Int64

A number that is greater than Int64.MinValue, but less than or equal to Int64.MaxValue.

Returns

A 64-bit signed integer, x, such that 0 ≤ x ≤ Int64.MaxValue.

Exceptions

Examples

The following example uses the Abs(Int64) method to get the absolute value of a number of Int64 values.

C#
long[] values = { Int64.MaxValue, 109013, 0, -6871982, Int64.MinValue };
foreach (long value in values)
{
   try {
      Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
   }
   catch (OverflowException) {
      Console.WriteLine("Unable to calculate the absolute value of {0}.",
                        value);
   }
}

// The example displays the following output:
//       Abs(9223372036854775807) = 9223372036854775807
//       Abs(109013) = 109013
//       Abs(0) = 0
//       Abs(-6871982) = 6871982
//       Unable to calculate the absolute value of -9223372036854775808.

Remarks

The absolute value of an Int64 is its numeric value without its sign. For example, the absolute value of both 123 and -123 is 123.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Abs(IntPtr)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the absolute value of a native signed integer.

C#
public static nint Abs(nint value);
C#
public static IntPtr Abs(IntPtr value);

Parameters

value

nint

A number that is greater than MinValue, but less than or equal to MaxValue.

Returns

nint

A native signed integer, x, such that 0 ≤ x ≤ MaxValue.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Abs(SByte)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Important

This API is not CLS-compliant.

Returns the absolute value of an 8-bit signed integer.

C#
[System.CLSCompliant(false)]
public static sbyte Abs(sbyte value);

Parameters

value
SByte

A number that is greater than SByte.MinValue, but less than or equal to SByte.MaxValue.

Returns

An 8-bit signed integer, x, such that 0 ≤ x ≤ SByte.MaxValue.

Attributes

Exceptions

Examples

The following example uses the Abs(SByte) method to get the absolute value of a number of SByte values.

C#
sbyte[] values = { SByte.MaxValue, 98, 0, -32, SByte.MinValue };
foreach (sbyte value in values)
{
   try {
      Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
   }
   catch (OverflowException) {
      Console.WriteLine("Unable to calculate the absolute value of {0}.",
                        value);
   }
}

// The example displays the following output:
//       Abs(127) = 127
//       Abs(98) = 98
//       Abs(0) = 0
//       Abs(-32) = 32
//       Unable to calculate the absolute value of -128.

Remarks

The absolute value of a signed byte is its numeric value without its sign. For example, the absolute value of both 12 and -12 is 12.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Abs(Single)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Returns the absolute value of a single-precision floating-point number.

C#
public static float Abs(float value);

Parameters

value
Single

A number that is greater than or equal to Single.MinValue, but less than or equal to Single.MaxValue.

Returns

A single-precision floating-point number, x, such that 0 ≤ x ≤ Single.MaxValue.

Examples

The following example uses the Abs(Single) method to get the absolute value of a number of Single values.

C#
float[] values= { Single.MaxValue, 16.354e-12F, 15.098123F, 0F,
                  -19.069713F, -15.058e17F, Single.MinValue };
foreach (float value in values)
   Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");

// The example displays the following output:
//       Abs(3.402823E+38) = 3.402823E+38
//       Abs(1.6354E-11) = 1.6354E-11
//       Abs(15.09812) = 15.09812
//       Abs(0) = 0
//       Abs(-19.06971) = 19.06971
//       Abs(-1.5058E+18) = 1.5058E+18
//       Abs(-3.402823E+38) = 3.402823E+38

Remarks

The absolute value of a Single is its numeric value without its sign. For example, the absolute value of both 1.2e-03 and -1.2e03 is 1.2e03.

If value is equal to NegativeInfinity or PositiveInfinity, the return value is PositiveInfinity. If value is equal to NaN, the return value is NaN.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0