Export (0) Print
Expand All

Math.Abs Method (Int32)

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

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()
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 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

Community Additions

ADD
Show:
© 2014 Microsoft