Decimal.Truncate() Method in C#


The Decimal.Truncate() method in C# is used to return the integral digits of the specified Decimal. Remember, any fractional digits are discarded.

Syntax

Following is the syntax −

public static decimal Truncate (decimal val);

Above, Val is the decimal number to truncate.

Example

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

using System;
public class Demo {
   public static void Main(){
      Decimal val = 6576.876m;
      Console.WriteLine("Decimal value = "+val);
      ulong res = Decimal.ToUInt64(val);
      Console.WriteLine("64-bit unsigned integer = "+res);
      Decimal val2 = Decimal.Truncate(val);
      Console.WriteLine("Integral digits of the decimal = "+val2);
   }
}

Output

This will produce the following output −

Decimal value = 6576.876
64-bit unsigned integer = 6576
Integral digits of the decimal = 6576

Example

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

using System;
public class Demo {
   public static void Main(){
      Decimal val = 28676.935m;
      Console.WriteLine("Decimal value = "+val);
      Decimal val2 = Decimal.Truncate(val);
      Console.WriteLine("Integral digits of the decimal = "+val2);
   }
}

Output

This will produce the following output −

Decimal value = 28676.935
Integral digits of the decimal = 28676

Updated on: 07-Nov-2019

883 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements