C# Math Abs() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.Abs(value) returns the absolute value of a number. That is the number without its sign: -5 becomes 5, and 5 stays 5.

Think of a ruler. The number -5 is five steps to the left of zero, and 5 is five steps to the right. Both are five steps away, and Abs gives you that distance. It is the easy way to ask "how far apart are these two numbers?" without caring which one is bigger.

This is a static member of the static class System.Math. You call it with the class name, such as Math.Abs(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Abs has been in .NET since the first version, with overloads for int, long, double, decimal, and more.

Syntax

Pass one number. You get back a number of the same type, with the minus sign removed.

C#
int a = Math.Abs(-5);          // 5
double b = Math.Abs(-3.5);     // 3.5
long c = Math.Abs(-9000000000L);  // 9000000000
CallWhat it does
Math.Abs(-8)Returns 8 (an int).
Math.Abs(-3.0)Returns 3 (a double, printed as 3).
Math.Abs(int.MinValue)Throws OverflowException. The positive twin does not fit in an int.
Math.Abs(double.NaN)Returns NaN. A double never throws here.

The result has the same type as the input, so Math.Abs(-5) is an int and Math.Abs(-5.0) is a double. The method does not change your variable. Store the answer if you want to keep it: x = Math.Abs(x);.

Library declarations
C#
public static int Abs(int value);
public static long Abs(long value);
public static float Abs(float value);
public static double Abs(double value);
public static decimal Abs(decimal value);
// also short, sbyte, and nint overloads

Parameters

ParameterDescription
valueRequired. The number to convert. It can be a negative, zero, or positive int, long, double, decimal, and so on.

Return Value

Return ValueDescription
Same type as valueThe number without its sign. For a double, -0.0 gives 0, infinity gives Infinity, and NaN gives NaN.

C# Examples

Example 1 — Remove the Minus Sign

Call Abs on negative, positive, and zero values, and one double.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.Abs(-5));
        Console.WriteLine(Math.Abs(5));
        Console.WriteLine(Math.Abs(0));
        Console.WriteLine(Math.Abs(-3.0));
    }
}

How It Works

  1. Negative becomes positive. Math.Abs(-5) returns 5. The sign is dropped and the size stays.
  2. Positive is unchanged. Math.Abs(5) also returns 5. Calling Abs on a number that is already positive is always safe.
  3. Zero stays zero. Zero has no sign to remove, so you get 0 back.
  4. Doubles work too. -3.0 is a double, so the double overload runs. The result is exactly 3, and Console prints it as 3.

Example 2 — Distance Between Two Numbers

Use Abs on a subtraction to get a gap, then to compare two doubles that are nearly equal.

C#
using System;

class Program {
    static void Main() {
        int morning = 25;
        int evening = 18;
        Console.WriteLine(Math.Abs(morning - evening));

        double sum = 0.1 + 0.2;
        Console.WriteLine(sum == 0.3);
        Console.WriteLine(Math.Abs(sum - 0.3) < 0.000001);
    }
}

How It Works

  1. Gap of two temperatures. morning - evening is 7 here, but it would be -7 if you swapped them. Abs makes the answer 7 whichever way round the numbers are.
  2. Doubles are not exact. 0.1 + 0.2 is stored as 0.30000000000000004, so sum == 0.3 is False.
  3. Compare with a tolerance. Math.Abs(sum - 0.3) is the size of the gap, a tiny number near 0.00000000000000006. We ask if that gap is smaller than 0.000001.
  4. True means "close enough". This is the standard way to compare doubles. Choose a tolerance that fits your problem, such as 0.000001 here.

Example 3 — int.MinValue, long, Infinity, and NaN

See the one case that throws, how a long avoids it, and how double special values behave.

C#
using System;

class Program {
    static void Main() {
        try {
            Console.WriteLine(Math.Abs(int.MinValue));
        } catch (OverflowException) {
            Console.WriteLine("overflow");
        }

        Console.WriteLine(Math.Abs((long)int.MinValue));
        Console.WriteLine(Math.Abs(double.NegativeInfinity));
        Console.WriteLine(Math.Abs(double.NaN));
    }
}

How It Works

  1. The int overload throws. The smallest int is -2147483648, but the largest is only 2147483647. The positive value does not fit, so Abs throws OverflowException and the catch block prints a word.
  2. Cast to long first. (long)int.MinValue picks the long overload, which has plenty of room. The answer is 2147483648.
  3. Infinity. The absolute value of negative infinity is positive infinity. Doubles never throw, they just carry on.
  4. NaN stays NaN. "Not a number" has no size, so Abs returns NaN again. Use double.IsNaN if you need to detect it.

Frequently Asked Questions

It returns the absolute value of a number, which is the number without its sign. Math.Abs(-5) is 5, Math.Abs(5) is 5, and Math.Abs(0) is 0.
The int range is -2147483648 to 2147483647, so +2147483648 does not fit in an int. Math.Abs throws OverflowException. Convert to long first, as in Math.Abs((long)value), to avoid it.
Subtract them and wrap the result in Math.Abs, as in Math.Abs(a - b). The answer is the same whichever number is bigger. For int values near the limits, convert to long first so the subtraction cannot overflow.
No. Math is a static class, so you call Math.Abs(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.
Yes. Abs has overloads for int, long, float, double, decimal, and several smaller number types. The result has the same type as the number you pass in.

Next: Acos()

Return the angle (in radians) whose cosine is a given number.

Acos() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful