MathF.Truncate() Method in C# with Examples

CsharpServer Side ProgrammingProgramming

The MathF.Truncate() method in C# is used to calculate an integral part of a specified single number or single-precision floating-point number.

Syntax

Following is the syntax −

public static float Truncate (float val);

Above, Val is the specified number to be truncated

Example

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

using System;
public class Demo {
   public static void Main(){
      float val1 = float.PositiveInfinity;
      float val2 = float.NegativeInfinity;
      Console.WriteLine("MathF.Truncate(val1) = "+MathF.Truncate(val1));
      Console.WriteLine("MathF.Truncate(val2) = "+MathF.Truncate(val2));
   }
}

Output

This will produce the following output −

MathF.Truncate(val1) = Infinity
MathF.Truncate(val2) = -Infinity

Example

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

using System;
public class Demo {
   public static void Main(){
      float val1 = 76.8787f;
      float val2 = 4646.5656f;
      Console.WriteLine("MathF.Truncate(val1) = "+MathF.Truncate(val1));
      Console.WriteLine("MathF.Truncate(val2) = "+MathF.Truncate(val2));
   }
}

Output

This will produce the following output −

MathF.Truncate(val1) = 76
MathF.Truncate(val2) = 4646
raja
Published on 05-Nov-2019 07:47:07
Advertisements