Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

decimal
truncate
c#
communitycreator

What is the Decimal.Truncate() method in C#?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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));
}
}
Get Only Integral Part of a Decimal in C#

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.

RELATED TAGS

decimal
truncate
c#
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring