What is the Decimal.Truncate() method in C#?
Overview
The Decimal.Truncate() method in C# is used to return the whole number part of a decimal number, while the fractional part is discarded.
Syntax
public static decimal Truncate (decimal d);
Parameters
d: The decimal value whose integral part we want to return.
Example
// use Systemusing System;// create classclass DecimalToSByte{// main methodstatic void Main(){// create decimal valuesdecimal d1 = 34.5m;decimal d2 = 2.4356345m;decimal d3 = 10.34m;// convert to SByte// and print valueConsole.WriteLine("Decimal Value = {0}, whole number part = {1}",d1,Decimal.Truncate(d1));Console.WriteLine("Decimal Value = {0}, whole number part = {1}",d2,Decimal.Truncate(d2));Console.WriteLine("Decimal Value = {0}, whole number part = {1}",d3,Decimal.Truncate(d3));}}
Explanation
- Lines 10 to 12: We create some decimal values.
- Lines 16 to 26: We call the
Truncate()method on the decimal values and print their results.