+ 1
When to use Math.Abs() .can u please explain me with example. Thank u.
3 Answers
+ 2
Just return the absolute value of a numeric value ( only positive values acepted) :
Sample from official doc
// The example displays the following output:
// Abs(79228162514264337593543950335) = 79228162514264337593543950335
// Abs(12.45) = 12.45
// Abs(0) = 0
// Abs(-19.69) = 19.69
// Abs(-79228162514264337593543950335) = 79228162514264337593543950335
+ 1
// The following loop will work right, no matter if num > 0, or num < 0
int num = -5;
for (int i = 0; i < Math.Abs(num); i++)
{
Console.WriteLine(i);
}
+ 1
Math.Abs() will generate the absolute value of a given term. In mathematics, absolute value is the non-negative value of a number without regard to its sign. In simple terms, how far the number is from zero, regardless of the directions. Thus, -5 and 5 and 5 away from 0. Therefore, their absolute value is 5.