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 System
using System;
// create class
class DecimalToSByte
{
// main method
static void Main()
{
// create decimal values
decimal d1 = 34.5m;
decimal d2 = 2.4356345m;
decimal d3 = 10.34m;
// convert to SByte
// and print value
Console.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.

Free Resources