Math.Truncate() Method in C#


The Math.Truncate() method in C# is used to compute an integral part of a number, which is Double or Decimal.

Syntax

public static decimal Truncate(decimal val1)
public static double Truncate(double val2)

Above, there are two syntaxes. The value val1 is the decimal number to truncate, whereas val2 is the double number to truncate.

Example

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

using System;
public class Demo {
   public static void Main(){
      Decimal val1 = 25.46467m;
      Decimal val2 = 45.9989m;
      Decimal val3 = 678.325m;
      Console.WriteLine(Math.Truncate(val1));
      Console.WriteLine(Math.Truncate(val2));
      Console.WriteLine(Math.Truncate(val3));
   }
}

Output

This will produce the following output −

25
45
678

Example

Let us see another example to implement Math.Truncate() method −

using System;
public class Demo {
   public static void Main(){
      Double val1 = 95.86467;
      Double val2 = 25.11;
      Double val3 = 878.325;
      Console.WriteLine(Math.Truncate(val1));
      Console.WriteLine(Math.Truncate(val2));
      Console.WriteLine(Math.Truncate(val3));
   }
}

Output

This will produce the following output −

95
25
878

Updated on: 05-Nov-2019

411 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements