Decimal.Truncate() Method in C#
Last Updated :
17 Feb, 2019
This method is used to get the integral digits of the specified Decimal by discarding any fractional digits. This method rounds the specified value to the nearest whole number by removing the digits after the decimal point.
Syntax: public static decimal Truncate (decimal d);
Here, d is the decimal number which is to be truncated.
Return Value: It returns the result of d rounded toward zero, to the nearest whole number.
Example:
using System;
class GFG {
public static void Main()
{
try {
Decimal dec1 = 214748.78549M;
Decimal dec2 = 21458565.2996m;
Decimal val1 = Decimal.Truncate(dec1);
Decimal val2 = Decimal.Truncate(dec2);
Console.WriteLine( "The integral part of " +
"dec1 is: {0}" , val1);
Console.WriteLine( "The integral part of " +
"dec2 is: {0}" , val2);
}
catch (OverflowException e) {
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
The integral part of dec1 is: 214748
The integral part of dec2 is: 21458565
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...