How to display Absolute value of a number in C#?

To find the absolute value of a number in C#, use the Math.Abs method. The absolute value represents the distance of a number from zero on the number line, always returning a non-negative result.

Syntax

The Math.Abs method is overloaded to work with different numeric types −

Math.Abs(int value)
Math.Abs(long value)
Math.Abs(float value)
Math.Abs(double value)
Math.Abs(decimal value)

Parameters

  • value − The number whose absolute value is to be computed.

Return Value

Returns the absolute value of the specified number. If the number is positive or zero, it returns the same value. If the number is negative, it returns the positive equivalent.

Math.Abs() Function Input -88 negative Output 88 positive Math.Abs()

Using Math.Abs with Integer Values

Example

using System;

class Program {
   static void Main() {
      int val1 = 77;
      int val2 = -88;

      Console.WriteLine("Before...");
      Console.WriteLine("val1: " + val1);
      Console.WriteLine("val2: " + val2);
      
      int abs1 = Math.Abs(val1);
      int abs2 = Math.Abs(val2);
      
      Console.WriteLine("After...");
      Console.WriteLine("Absolute value of " + val1 + ": " + abs1);
      Console.WriteLine("Absolute value of " + val2 + ": " + abs2);
   }
}

The output of the above code is −

Before...
val1: 77
val2: -88
After...
Absolute value of 77: 77
Absolute value of -88: 88

Using Math.Abs with Different Data Types

Example

using System;

class Program {
   static void Main() {
      double doubleValue = -45.67;
      float floatValue = -12.34f;
      decimal decimalValue = -99.99m;
      long longValue = -1234567890L;

      Console.WriteLine("Original values:");
      Console.WriteLine("Double: " + doubleValue);
      Console.WriteLine("Float: " + floatValue);
      Console.WriteLine("Decimal: " + decimalValue);
      Console.WriteLine("Long: " + longValue);

      Console.WriteLine("\nAbsolute values:");
      Console.WriteLine("Double: " + Math.Abs(doubleValue));
      Console.WriteLine("Float: " + Math.Abs(floatValue));
      Console.WriteLine("Decimal: " + Math.Abs(decimalValue));
      Console.WriteLine("Long: " + Math.Abs(longValue));
   }
}

The output of the above code is −

Original values:
Double: -45.67
Float: -12.34
Decimal: -99.99
Long: -1234567890

Absolute values:
Double: 45.67
Float: 12.34
Decimal: 99.99
Long: 1234567890

Common Use Cases

  • Distance calculations − Finding the distance between two points.

  • Error measurements − Calculating absolute differences in scientific computations.

  • Data validation − Ensuring positive values for certain business logic.

Conclusion

The Math.Abs method in C# provides a simple way to obtain the absolute value of numeric data types. It automatically handles different numeric types and always returns a non-negative result, making it essential for mathematical calculations and data processing scenarios.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements