Truncate method of Math class is used to find integral part of a specified decimal number or double-precision floating-point number. You can find decimal part of a specified decimal number as well by using Truncate method.
Getting Integral Part of Specified Number by C#
Syntax:
Math.Truncate(specifiedNumber);
Truncate method trims decimal values and returns the whole number part of specifiedNumber. This works on both data type double and decimal. If data type of specifiedNumber is double, it will return the value of double data type and if data type of specifiedNumber is decimal, it will return the value of decimal data type. Below C# example code illustrates its functionality more clearly.
// C# Sample Program for Math.Truncate(Decimal) Method
using System;
class CFDExample
{
public static void Main()
{
// variable of decimal type
decimal specifiedNumber1 = 54.63565m;
// variable of double type
double specifiedNumber2 = 536784.45178;
// Printing integral part of specified number
Console.WriteLine(Math.Truncate(specifiedNumber1));
Console.WriteLine(Math.Truncate(specifiedNumber2));
}
}
This function removes the fractional part of a specifiedNumber.
Output:
54
536784
Getting fractional Part of Specified Number by C#
Using Math.Truncate() in below given way, you can get decimal part of specified decimal number.
Syntax:
specifiedNumber – Math.Truncate(specifiedNumber);
// C# Sample Program for getting decimal part using Math.Truncate(Decimal) Method
using System;
class CFDExample
{
public static void Main()
{
// variable of decimal type
decimal specifiedNumber1 = 54.63565m;
// variable of double type
double specifiedNumber2 = 536784.45178;
// Printing decimal part of specified number
Console.WriteLine(specifiedNumber1 - Math.Truncate(specifiedNumber1));
Console.WriteLine(specifiedNumber2 - Math.Truncate(specifiedNumber2));
}
}
Output:
0.63565
0.45178