Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
math
communitycreator

What is Math.Abs() in C#?

Arslan Bajwa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

C# has a built-in Math class that provides useful mathematical functions and operations. The class has the Abs() function, which is used to compute the absolute value of a specified number.

Syntax

Abs (type param);

where:

  • param is a number greater than or equal to MinValue and less than or equal to MaxValue.

Return value

Returns a value such that:

  • 0 <= value <= MaxValue

MinValue and MaxValue are equal to the minimum and maximum value of integer that can be stored in that specific type.

Variants

Abs(Decimal)

This function returns the absolute value of a decimal number.

  public static decimal Abs (decimal param);

Abs(Double)

This function returns the absolute value of a double-precision floating-point number.

  public static double Abs (double param);

Abs(Int16)

This function returns the absolute value of a 16-bit signed number.

  public static short Abs (short value);

Abs(Int32)

This function returns the absolute value of a 32-bit signed number.

  public static int Abs (int param);

Abs(Int64)

This function returns the absolute value of a 64-bit signed number.

  public static long Abs (long param);

Abs(SByte)

This function returns the absolute value of an 8-bit signed number.

  public static sbyte Abs (sbyte param);

Abs(Single)

This function returns the absolute value of a single-precision floating-point number.

  public static float Abs (float param);

Code

using System;
class HelloWorld
{
static void Main()
{
System.Console.WriteLine(Math.Abs(-2));
System.Console.WriteLine(Math.Abs(-58.2M));
System.Console.WriteLine(Math.Abs(-58.2));
System.Console.WriteLine(Math.Abs(0.0));
}
}

RELATED TAGS

c#
math
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring