1 out of 2 rated this helpful - Rate this topic

# Math.Abs Method (Int32)

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

```public static int Abs (
int value
)
```
```public static int Abs (
int value
)
```
```public static function Abs (
value : int
) : int
```
```Not applicable.
```

#### Parameters

value

A number in the range MinValue < valueMaxValue.

#### Return Value

A 32-bit signed integer, x, such that 0 x MaxValue.
Exception typeCondition

OverflowException

value equals MinValue.

The following example demonstrates how to use the Abs method to display the absolute values of a list of variables.

```// This example demonstrates Math.Abs()
using System;

class Sample
{
public static void Main()
{
sbyte    sb1 = -16, sb2 = 16;
short    sh1 = -15, sh2 = 15;
int      in1 = -14, in2 = 14;
long     lg1 = -13, lg2 = 13;
float    fl1 = -12.0f, fl2 = 12.0f;
double   db1 = -11.1, db2 = 11.1;
Decimal  de1 = -10.0m, de2 = 10.0m;

Console.WriteLine();
Console.WriteLine("SByte:   1) {0,-5} 2) {1,-5}", Math.Abs(sb1), Math.Abs(sb2));
Console.WriteLine("Int16:   1) {0,-5} 2) {1,-5}", Math.Abs(sh1), Math.Abs(sh2));
Console.WriteLine("Int32:   1) {0,-5} 2) {1,-5}", Math.Abs(in1), Math.Abs(in2));
Console.WriteLine("Int64:   1) {0,-5} 2) {1,-5}", Math.Abs(lg1), Math.Abs(lg2));
Console.WriteLine("Single:  1) {0,-5} 2) {1,-5}", Math.Abs(fl1), Math.Abs(fl2));
Console.WriteLine("Double:  1) {0,-5} 2) {1,-5}", Math.Abs(db1), Math.Abs(db2));
Console.WriteLine("Decimal: 1) {0,-5} 2) {1,-5}", Math.Abs(de1), Math.Abs(de2));
}
}
/*
This example produces the following results:

SByte:   1) 16    2) 16
Int16:   1) 15    2) 15
Int32:   1) 14    2) 14
Int64:   1) 13    2) 13
Single:  1) 12    2) 12
Double:  1) 11.1  2) 11.1
Decimal: 1) 10.0  2) 10.0
*/

```
```// This example demonstrates Math.Abs()
import System.*;

class Sample
{
public static void main(String[] args)
{
byte sb1 = -16;
byte sb2 = 16;
short sh1 = -15;
short sh2 = 15;
int in1 = -14;
int in2 = 14;
long lg1 = -13;
long lg2 = 13;
float fl1 = -12;
float fl2 = 12;
double db1 = -11.1;
double db2 = 11.1;
Decimal de1 = new Decimal(-10.0);
Decimal de2 = new Decimal(10.0);

Console.WriteLine();
Console.WriteLine("SByte:   1) {0,-5} 2) {1,-5}",
System.Convert.ToString(System.Math.Abs(sb1)),
System.Convert.ToString(System.Math.Abs(sb2)));
Console.WriteLine("Int16:   1) {0,-5} 2) {1,-5}",
System.Convert.ToString(System.Math.Abs(sh1)),
System.Convert.ToString(System.Math.Abs(sh2)));
Console.WriteLine("Int32:   1) {0,-5} 2) {1,-5}",
System.Convert.ToString(System.Math.Abs(in1)),
System.Convert.ToString(System.Math.Abs(in2)));
Console.WriteLine("Int64:   1) {0,-5} 2) {1,-5}",
System.Convert.ToString(System.Math.Abs(lg1)),
System.Convert.ToString(System.Math.Abs(lg2)));
Console.WriteLine("Single:  1) {0,-5} 2) {1,-5}",
System.Convert.ToString(System.Math.Abs(fl1)),
System.Convert.ToString(System.Math.Abs(fl2)));
Console.WriteLine("Double:  1) {0,-5} 2) {1,-5}",
System.Convert.ToString(System.Math.Abs(db1)),
System.Convert.ToString(System.Math.Abs(db2)));
Console.WriteLine("Decimal: 1) {0,-5} 2) {1,-5}",
System.Convert.ToString(System.Math.Abs(de1)),
System.Convert.ToString(System.Math.Abs(de2)));
} //main
} //Sample

/*
This example produces the following results:

SByte:   1) 16    2) 16
Int16:   1) 15    2) 15
Int32:   1) 14    2) 14
Int64:   1) 13    2) 13
Single:  1) 12    2) 12
Double:  1) 11.1  2) 11.1
Decimal: 1) 10    2) 10
*/

```

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

#### .NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

#### .NET Compact Framework

Supported in: 2.0, 1.0

#### XNA Framework

Supported in: 1.0

Math Class
Math Members
System Namespace