C# Math Abs(Int32)

Description

Math Abs(Int32) returns the absolute value of a 32-bit signed integer.

Syntax

Math.Abs(Int32) has the following syntax.


public static int Abs(
  int value
)

Parameters

Math.Abs(Int32) has the following parameters.

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

Returns

Math.Abs(Int32) method returns A 32-bit signed integer, x, such that 0 d x d Int32.MaxValue.

Example

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


/*from  w  w w  .jav  a 2s. com*/
using System;

public class Example
{
   public static void Main()
   {
      int[] values = { Int32.MaxValue, 12345, 0, -987658, Int32.MinValue };
      foreach (int value in values)
      {
         try {
            Console.WriteLine("Abs({0}) = {1}", value, Math.Abs(value));
         }   
         catch (OverflowException) {
            Console.WriteLine("Unable to calculate the absolute value of {0}.", 
                              value);
         }
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version