Export (0) Print
Expand All

Math.Abs Method (Decimal)

.NET Framework 1.1

Returns the absolute value of a Decimal number.

[Visual Basic]
Overloads Public Shared Function Abs( _
   ByVal value As Decimal _
) As Decimal
[C#]
public static decimal Abs(
 decimal value
);
[C++]
public: static Decimal Abs(
 Decimal value
);
[JScript]
public static function Abs(
   value : Decimal
) : Decimal;

Parameters

value
A number in the range MinValue ≤ value ≤ MaxValue.

Return Value

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

Example

[Visual Basic] 
' 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
'

[C#] 
// 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
*/

[C++] 
// This example demonstrates Math.Abs()
#using <mscorlib.dll>
using namespace System;

int main()
{
    SByte    sb1 = -16, sb2 = 16;
    Int16    sh1 = -15, sh2 = 15;
    Int32    in1 = -14, in2 = 14;
    Int64    lg1 = -13, lg2 = 13;
    Single   fl1 = -12.0f, fl2 = 12.0f;
    Double   db1 = -11.1, db2 = 11.1;
    Decimal  de1 = Decimal::Parse(S"-10.0"), de2 = Decimal::Parse(S"10.0");

    Console::WriteLine();
    Console::WriteLine(S"SByte:   1) {0,-5} 2) {1,-5}", __box(Math::Abs(sb1)), __box(Math::Abs(sb2)));
    Console::WriteLine(S"Int16:   1) {0,-5} 2) {1,-5}", __box(Math::Abs(sh1)), __box(Math::Abs(sh2)));
    Console::WriteLine(S"Int32:   1) {0,-5} 2) {1,-5}", __box(Math::Abs(in1)), __box(Math::Abs(in2)));
    Console::WriteLine(S"Int64:   1) {0,-5} 2) {1,-5}", __box(Math::Abs(lg1)), __box(Math::Abs(lg2)));
    Console::WriteLine(S"Single:  1) {0,-5} 2) {1,-5}", __box(Math::Abs(fl1)), __box(Math::Abs(fl2)));
    Console::WriteLine(S"Double:  1) {0,-5} 2) {1,-5}", __box(Math::Abs(db1)), __box(Math::Abs(db2)));
    Console::WriteLine(S"Decimal: 1) {0,-5} 2) {1,-5}", __box(Math::Abs(de1)), __box(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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Math Class | Math Members | System Namespace | Math.Abs Overload List

Show:
© 2014 Microsoft