This documentation is archived and is not being maintained.

Math.Abs Method (Int32)

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

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

'Declaration
Public Shared Function Abs ( _
	value As Integer _
) As Integer
'Usage
Dim value As Integer
Dim returnValue As Integer

returnValue = Math.Abs(value)
public static int Abs (
	int value
)
public static function Abs (
	value : int
) : int

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.

' This example demonstrates Math.Abs()
Imports System

Class Sample
   Public Shared Sub Main()
      ' Dim sb1 As System.SByte = - 16 'Signed Bytes are not supported.
      ' Dim sb2 As System.SByte = 16   'Signed Bytes are not supported.
      Dim sh1 As Short = - 15
      Dim sh2 As Short = 15
      Dim in1 As Integer = - 14
      Dim in2 As Integer = 14
      Dim lg1 As Long = - 13
      Dim lg2 As Long = 13
      Dim fl1 As Single = - 12F
      Dim fl2 As Single = 12F
      Dim db1 As Double = - 11.1
      Dim db2 As Double = 11.1
      Dim de1 As [Decimal] = - 10D
      Dim de2 As [Decimal] = 10D
      
      Console.WriteLine()
      ' Signed bytes are not supported.
      ' 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))
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: