C# Math.Abs() – Examples
In this tutorial, we will learn about the C# Math.Abs() method, and learn how to use this method to find the absolute value of given number, with the help of examples.
Abs(Decimal)
Math.Abs(value) returns the absolute value of a decimal number value
.
Syntax
The syntax of Abs(value) method is
Math.Abs(Decimal value)
where
Parameter | Description |
---|---|
value | The decimal value whose absolute value has to be found out. |
Return Value
Abs(Decimal) returns decimal value.
Example 1 – Abs(Decimal)
In this example, we will take few decimal values and find out their absolute value using Math.Abs(Decimal) method.
C# Program
using System; class Example { static void Main(string[] args) { Decimal value; value = -45.55M; Console.WriteLine(Math.Abs(value)); value = 0.0M; Console.WriteLine(Math.Abs(value)); value = -58.2M; Console.WriteLine(Math.Abs(value)); } }
Output
45.55 0.0 58.2
Abs(Double)
Math.Abs(value) returns the absolute value of a double-precision floating-point number value
.
Syntax
The syntax of Abs(Double) method is
Math.Abs(Double value)
where
Parameter | Description |
---|---|
value | The Double value whose absolute value has to be found out. |
Return Value
Abs(Double) returns double value.
Example 2 – Abs(Double)
In this example, we will take few double values and find out their absolute value using Math.Abs(Double) method.
C# Program
using System; class Example { static void Main(string[] args) { Double value; value = -45.55; Console.WriteLine(Math.Abs(value)); value = 0.0; Console.WriteLine(Math.Abs(value)); value = -58.2; Console.WriteLine(Math.Abs(value)); value = 8.7; Console.WriteLine(Math.Abs(value)); value = -55.55e-10; Console.WriteLine(Math.Abs(value)); value = Double.NaN; Console.WriteLine(Math.Abs(value)); } }
Output
45.55 0 58.2 8.7 5.555E-09 NaN
Abs(Int16)
Math.Abs(value) returns the absolute value of a 16-bit signed integer value
.
Syntax
The syntax of Abs(Int16) method is
Math.Abs(Int16 value)
where
Parameter | Description |
---|---|
value | The 16-bit signed integer whose absolute value has to be found out. |
Return Value
Abs(Int16) returns 16-bit signed integer value.
Example 3 – Abs(Int16)
In this example, we will take few 16-bit signed integers and find out their absolute value using Math.Abs(Int16) method.
C# Program
using System; class Example { static void Main(string[] args) { Int16 value; value = -45; Console.WriteLine(Math.Abs(value)); value = 7; Console.WriteLine(Math.Abs(value)); value = -58; Console.WriteLine(Math.Abs(value)); } }
Output
45 7 58
Abs(Int32)
Math.Abs(value) returns the absolute value of a 32-bit signed integer value
.
Syntax
The syntax of Abs() method is
Math.Abs(Int32 value)
where
Parameter | Description |
---|---|
value | The 32-bit signed integer whose absolute value has to be found out. |
Return Value
Abs(Decimal) returns 32-bit signed integer value.
Example 4 – Abs(Int32)
In this example, we will take few 32-bit signed integer values and find out their absolute value using Math.Abs(Int32) method.
C# Program
using System; class Example { static void Main(string[] args) { Int32 value; value = -455844525; Console.WriteLine(Math.Abs(value)); value = 7; Console.WriteLine(Math.Abs(value)); value = -58; Console.WriteLine(Math.Abs(value)); } }
Output
455844525 7 58
Abs(Int64)
Math.Abs(value) returns the absolute value of a 64-bit signed integer value
.
Syntax
The syntax of Abs() method is
Math.Abs(Int64 value)
where
Parameter | Description |
---|---|
value | The 64-bit signed integer whose absolute value has to be found out. |
Return Value
Abs(Int64) returns 64-bit signed integer value.
Example 5 – Abs(Int64)
In this example, we will take few 64-bit signed integer values and find out their absolute value using Math.Abs(Int64) method.
C# Program
using System; class Example { static void Main(string[] args) { Int64 value; value = -9455844525; Console.WriteLine(Math.Abs(value)); value = 7; Console.WriteLine(Math.Abs(value)); value = -58; Console.WriteLine(Math.Abs(value)); } }
Output
9455844525 7 58
Abs(SByte)
Math.Abs(value) returns the absolute value of an 8-bit signed integer value
.
Syntax
The syntax of Abs() method is
Math.Abs(SByte value)
where
Parameter | Description |
---|---|
value | The 8-bit signed integer value whose absolute value has to be found out. |
Return Value
Abs(SByte) returns 8-bit signed integer.
Example 6 – Abs(SByte)
In this example, we will take few 8-bit signed integers and find out their absolute value using Math.Abs(SByte) method.
C# Program
using System; class Example { static void Main(string[] args) { SByte value; value = -87; Console.WriteLine(Math.Abs(value)); value = 7; Console.WriteLine(Math.Abs(value)); value = -56; Console.WriteLine(Math.Abs(value)); } }
Output
87 7 56
Abs(Single)
Math.Abs(value) returns the absolute value of a single-precision floating-point number value
.
Syntax
The syntax of Abs(Single) method is
Math.Abs(Single value)
where
Parameter | Description |
---|---|
value | The single-precision floating-point number whose absolute value has to be found out. |
Return Value
Abs(Single) returns single-precision floating-point number.
Example 7 – Abs(Single)
In this example, we will take few single-precision floating-point numbers and find out their absolute value using Math.Abs(Single) method.
C# Program
using System; class Example { static void Main(string[] args) { Single value; value = -87; Console.WriteLine(Math.Abs(value)); value = 7; Console.WriteLine(Math.Abs(value)); value = -56; Console.WriteLine(Math.Abs(value)); } }
Output
87 7 56
Conclusion
In this C# Tutorial, we have learnt the syntax of C# Math.Abs() method, and also learnt how to use this method with the help of C# example programs.