Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Math.Abs() Method in C#
The Math.Abs() method in C# is used to return the absolute value of a specified number in C#. This specified number can be decimal, double, 16-bit signed integer, etc.
Example
Let us now see an example to implement the Math.abs() method to return the absolute value of double number −
using System;
class Demo {
public static void Main(){
Double val1 = 30.40;
Double val2 = Double.MinValue;
Double val3 = Double.MaxValue;
Console.WriteLine("Absolute value of {0} : {1}", val1, Math.Abs(val1));
Console.WriteLine("Absolute value of {0} : {1}", val2, Math.Abs(val2));
Console.WriteLine("Absolute value of {0} : {1}", val3, Math.Abs(val3));
}
}
Output
This will produce the following output −
Absolute value of 30.4 : 30.4 Absolute value of -1.79769313486232E+308 : 1.79769313486232E+308 Absolute value of 1.79769313486232E+308 : 1.79769313486232E+308
Example
Let us now see another example to implement the Math.abs() method to return the absolute value of 16-bit signed integer −
using System;
class Demo {
public static void Main(){
short val1 = -300;
short val2 = Int16.MaxValue;
short val3 = 0;
Console.WriteLine("Absolute value of {0} : {1}", val1, Math.Abs(val1));
Console.WriteLine("Absolute value of {0} : {1}" val2, Math.Abs(val2));
Console.WriteLine("Absolute value of {0} : {1}", val3, Math.Abs(val3));
}
}
Output
This will produce the following output −
Absolute value of -300 : 300 Absolute value of 32767 : 32767 Absolute value of 0 : 0
Advertisements