Open In App

C# | Math.Abs() Method | Set – 1

Last Updated : 01 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it.

  1. Math.Abs(Decimal)
  2. Math.Abs(Double)
  3. Math.Abs(Int16)
  4. Math.Abs(Int32)
  5. Math.Abs(Int64)
  6. Math.Abs(SByte)
  7. Math.Abs(Single)
  8. Math.Abs(Decimal)

    This method is used to return the absolute value of a Decimal number.

    Syntax:

    public static decimal Abs (decimal val);

    Parameter:

    val: It is the required number which is greater than or equal to Decimal.MinValue, but less than or equal to Decimal.MaxValue of type System.Decimal.

    Return Type: It returns a decimal number say r, such that 0 ≤ r ≤ Decimal.MaxValue.

    Example:




    // C# Program to illlustrate the
    // Math.Abs(Decimal) Method
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // Taking decimal values
            decimal[] deci = {Decimal.MinValue, 45.14M, 0M,
                                -17.47M, Decimal.MaxValue};
      
            // using foreach loop
            foreach(decimal value in deci)
      
                // Displaying the result
                Console.WriteLine("Absolute value of {0} = {1}",
                                        value, Math.Abs(value));
        }
    }

    
    

    Output:

    Absolute value of -79228162514264337593543950335 = 79228162514264337593543950335
    Absolute value of 45.14 = 45.14
    Absolute value of 0 = 0
    Absolute value of -17.47 = 17.47
    Absolute value of 79228162514264337593543950335 = 79228162514264337593543950335
    

    Math.Abs(Double)

    This method is used to return the absolute value of a double-precision floating-point number.

    Syntax:

    public static double Abs (double val);

    Parameter:

    val: It is the required number which is greater than or equal to Double.MinValue, but less than or equal to Double.MaxValue of type System.Double.

    Return Type: It returns a double-precision floating-point number say r, such that 0 ≤ r ≤ Double.MaxValue.

    Note:

    • If val is equal to NegativeInfinity or PositiveInfinity, the return value will be PositiveInfinity.
    • If the val is equal to NaN then return value will be NaN.

    Example:




    // C# Program to illlustrate the
    // Math.Abs(Double) Method
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // Taking a NaN
            Double nan = Double.NaN;
      
            // Taking double values
            double[] doub = {Double.MinValue, 27.58, 0.0,
                            56.48e10, nan, Double.MaxValue};
      
            // using foreach loop
            foreach(double value in doub)
      
                // Displaying the result
                Console.WriteLine("Absolute value of {0} = {1}",
                                        value, Math.Abs(value));
        }
    }

    
    

    Output:

    Absolute value of -1.79769313486232E+308 = 1.79769313486232E+308
    Absolute value of 27.58 = 27.58
    Absolute value of 0 = 0
    Absolute value of 564800000000 = 564800000000
    Absolute value of NaN = NaN
    Absolute value of 1.79769313486232E+308 = 1.79769313486232E+308
    

    Math.Abs(Int16)

    This method is used to return the absolute value of a 16-bit signed integer.

    Syntax:

    public static short Abs (short val);

    Parameter:

    val: It is the required number which is greater than Int16.MinValue, but less than or equal to Int16.MaxValue of type System.Int16.

    Return Type: It returns 16-bit signed integer say r, such that 0 ≤ r ≤ Int16.MaxValue.

    Exception: This method will give OverflowException if the value of val is equals to Int16.MinValue.

    Example:




    // C# Program to illlustrate the
    // Math.Abs(Int16) Method
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // Taking short values
            short[] sh = {Int16.MaxValue, 1482, -142, 0 };
      
            // using foreach loop
            foreach(short value in sh)
      
                // Displaying the result
                Console.WriteLine("Absolute value of {0} = {1}",
                                        value, Math.Abs(value));
        }
    }

    
    

    Output:

    Absolute value of 32767 = 32767
    Absolute value of 1482 = 1482
    Absolute value of -142 = 142
    Absolute value of 0 = 0
    

    Math.Abs(Int32)

    This method is used to return the absolute value of a 32-bit signed integer.

    Syntax:

    public static int Abs (int val);

    Parameter:

    val: It is the required number which is greater than Int32.MinValue, but less than or equal to Int32.MaxValue of type System.Int32.

    Return Type: It returns 32-bit signed integer say r, such that 0 ≤ r ≤ Int32.MaxValue.

    Exception: This method will give OverflowException if the value of val is equals to Int32.MinValue.

    Example:




    // C# Program to illlustrate the
    // Math.Abs(Int32) Method
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // Taking int values
            int[] int_val = {Int32.MaxValue, 13482, -65525, 0};
      
            // using foreach loop
            foreach(int value in int_val)
      
                // Displaying the result
                Console.WriteLine("Absolute value of {0} = {1}",
                                        value, Math.Abs(value));
        }
    }

    
    

    Output:

    Absolute value of 2147483647 = 2147483647
    Absolute value of 13482 = 13482
    Absolute value of -65525 = 65525
    Absolute value of 0 = 0
    

    There are total 7 methods in its overload list. Here we will discuss only the first 4 methods and remaining 3 methods are discussed in C# | Math.Abs() Method | Set – 2.

    Reference: https://docs.microsoft.com/en-us/dotnet/api/system.math.abs?view=netframework-4.7.2



Similar Reads

C# | Math.Abs() Method | Set - 2
C# | Math.Abs() Method | Set – 1 In C#, Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it. There are total 7 methods in its overload list. Math.Abs(Decimal) Math.Abs(Double) Math.Abs(Int16) Math.Abs(Int32) Math.Abs(Int64) Math.
3 min read
MathF.Abs() Method in C# with Examples
MathF.Abs(Single) Method is used to return the absolute value of a specified float number. Syntax: public static float Abs (float x); Here, it takes a standard floating point number. Return Value: This method returns a floating point value less than Single.MaxValue. Example 1: // C# program to demonstrate the // MathF.Abs(Single) Method using Syste
1 min read
C# | Math.Round() Method | Set - 1
In C#, Math.Round() is a Math class method which is used to round a value to the nearest integer or to the particular number of fractional digits. This method can be overloaded by changing the number and type of the arguments passed. There are total 8 methods in the overload list of the Math.Round() method. Here we will discuss only 4 methods and r
6 min read
C# | Math.Round() Method | Set – 2
In C#, Math.Round() is a Math class method which is used to round a value to the nearest integer or to the particular number of fractional digits. This method has another overload with which, you can specify the number of digits beyond the decimal point in the returned value. It returns the nearest value of the number with the precision equal to th
8 min read
C# | Math.IEEERemainder() Method
In C#, IEEERemainder() is a Math class method which is used to return the remainder resulting from the division of a specified number by another specified number. Syntax: public static double IEEERemainder (double a, double b); Parameters: a: It is the dividend of type System.Double.b: It is the divisor of type System.Double. Return Type: This meth
2 min read
C# | Math.Sign() Method
In C#, Sign() is a math class method which returns an integer that specify the sign of the number. This method can be overloaded by changing the data type of the passed arguments as follows: Math.Sign(Decimal): Returns the integer that specifies the sign of a decimal number. Math.Sign(Double): Returns the integer that specifies the sign of a double
3 min read
C# | Math.BigMul() Method
In C#, BigMul() is a method class method. This method is used to compute the full product of two 32-bit numbers.Syntax: public static long BigMul(int a, int b) Parameters: a: It is the first number to be multiplied and the type of this parameter is System.Int32. b: It is the Second number to be multiplied and the type of this parameter is System.In
1 min read
C# | Math.Pow() Method
In C#, Math.Pow() is a Math class method. This method is used to calculate a number raise to the power of some other number. Syntax: public static double Pow(double base, double power) Parameters: double base: It is a double-precision floating-point number which is to be raised to a power and type of this parameter is System.Double. double power: I
2 min read
C# | Math.Floor() Method
In C#, Math.Floor() is a Math class method. This method is used to find the largest integer, which is less than or equal to the passed argument. The floor method operates both functionalities in decimal and double. This method can be overload by passing different arguments to it. Math.Floor(Decimal) Method Math.Floor(Double) Method Math.Floor(Decim
3 min read
C# | Math.Tanh() Method
Math.Tanh() is the inbuilt Math class method which returns the hyperbolic tan of a given double value argument. The result will be NaN if the given argument is NaN.Syntax: public static double Tanh(double num) Parameter: num: It is the number whose hyperbolic tan is to be returned and type of this parameter is System.Double. Return Value: The metho
2 min read
Article Tags :