MathF.Abs() Method in C# with Examples


The MathF.Abs() method in C# is used to return the absolute value of a float number.

Syntax

Following is the syntax −

public static float Abs (float val);

Above, the parameter Val is the specified float number.

Example

Let us now see an example to implement the MathF.Abs() method −

using System;
public class Demo {
   public static void Main(){
      float val1 = 20.5f;
      float val2 = -89.5f;
      Console.WriteLine("Absolute float value1 = "+(MathF.Abs(val1)));
      Console.WriteLine("Absolute float value2 = "+(MathF.Abs(val2)));
   }
}

Output

This will produce the following output −

Absolute float value1 = 20.5
Absolute float value2 = 89.5

Example

Let us now see another example to implement the MathF.Abs() method −

using System;
public class Demo {
   public static void Main(){
      float val1 = 0.1f;
      float val2 = 1.0f;
      Console.WriteLine("Absolute float value1 = "+(MathF.Abs(val1)));
      Console.WriteLine("Absolute float value2 = "+(MathF.Abs(val2)));
   }
}

Output

This will produce the following output −

Absolute float value1 = 0.1
Absolute float value2 = 1

Updated on: 13-Nov-2019

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements