In C#, Math.Truncate(value)cuts off the decimal part of a number and keeps the whole part. Math.Truncate(2.9) is 2, and Math.Truncate(-2.9) is -2.
Think of a knife slicing off everything after the decimal point. There is no rounding at all, so 2.99999 still becomes 2. The direction is always toward zero. That is the difference from Floor(), which goes down, and Ceiling(), which goes up. For positive numbers, Truncate and Floor agree.
This is a static member of the static class System.Math. You call it with the class name, such as Math.Truncate(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Truncate is in every .NET version, with double and decimal overloads.
Syntax
Pass a double or decimal. You get the same type back, holding only the whole part.
C#
double a = Math.Truncate(2.9); // 2
double b = Math.Truncate(-2.9); // -2
decimal c = Math.Truncate(19.99m); // 19
Call
What it does
Math.Truncate(2.9)
Returns 2.
Math.Truncate(-2.9)
Returns -2, toward zero.
Math.Truncate(100.99)
Returns 100.
Math.Truncate(19.99m)
The decimal version returns 19.
The result is still a double or decimal. A cast like (int)2.9 also drops the decimals, but it can overflow or give a wrong number when the value does not fit in an int, while Math.Truncate keeps the bigger type. To get the fractional part, subtract: value - Math.Truncate(value).
Library declarations
C#
public static double Truncate(double d);
public static decimal Truncate(decimal d);
Parameters
Parameter
Description
d
Required. The double or decimal number to cut off.
Return Value
Return Value
Description
double or decimal
The whole part of d, moved toward zero. NaN and infinities are returned unchanged.
C# Examples
Example 1 — Keep Only the Whole Part
Truncate positive and negative numbers and see that no rounding happens.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.Truncate(2.9));
Console.WriteLine(Math.Truncate(-2.9));
Console.WriteLine(Math.Truncate(7.0));
Console.WriteLine(Math.Truncate(100.99));
}
}
Output
2
-2
7
100
How It Works
Positive. 2.9 loses its .9 and becomes 2. It is not rounded up.
Negative. -2.9 becomes -2, which is toward zero. Floor would have said -3.
Whole numbers. 7.0 has no decimal part to cut, so it stays 7.
Big decimal part. 100.99 still becomes 100. Truncate never looks at the digits it removes.
Example 2 — Split Dollars and Cents
Use Truncate to get the whole part of a price, then subtract to get the fractional part.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
double price = 12.75;
double dollars = Math.Truncate(price);
double fraction = price - dollars;
Console.WriteLine(dollars);
Console.WriteLine(fraction.ToString(CultureInfo.InvariantCulture));
Console.WriteLine(Math.Round(fraction * 100));
Console.WriteLine(Math.Truncate(-price));
}
}
Output
12
0.75
75
-12
How It Works
Whole part.Math.Truncate(12.75) is 12. These are the whole dollars.
Fractional part. Subtracting the whole part leaves 0.75. Both 12.75 and 12 can be stored exactly in a double, so the answer is exact.
Invariant culture. The ToString(CultureInfo.InvariantCulture) call forces a dot as the decimal mark, so the text is the same on every computer.
Cents and negatives. Multiplying by 100 and rounding gives 75 cents. For -12.75, Truncate keeps -12 and the sign stays with the whole part.
Example 3 — Truncate, the int Cast, and Floor
See that Truncate agrees with an (int) cast for small numbers, handles values too big for an int, and differs from Floor for negatives.
C#
using System;
class Program {
static void Main() {
double small = 7.9;
Console.WriteLine((int)small);
Console.WriteLine(Math.Truncate(small));
double big = 3000000000.9;
Console.WriteLine(Math.Truncate(big));
Console.WriteLine(Math.Truncate(-7.5));
Console.WriteLine(Math.Floor(-7.5));
Console.WriteLine(Math.Truncate(19.99m));
}
}
Output
7
7
3000000000
-7
-8
19
How It Works
Same answer for small numbers. Casting 7.9 with (int) and calling Math.Truncate both give 7. The difference is the type: int versus double.
Too big for an int. 3000000000.9 is larger than int.MaxValue. Math.Truncate returns a double and handles it fine: 3000000000. An (int) cast would give a wrong value.
Truncate versus Floor. For -7.5, Truncate moves toward zero and gives -7, while Floor moves down and gives -8.
decimal.Math.Truncate(19.99m) is 19. This is handy for money when you want whole units only.
Frequently Asked Questions
It removes the decimal part of a number and returns the whole part, moving toward zero. Math.Truncate(2.9) is 2 and Math.Truncate(-2.9) is -2.
Truncate moves toward zero and Floor moves toward negative infinity. They give the same answer for positive numbers. For -2.9, Truncate gives -2 and Floor gives -3.
For values that fit in an int, yes: (int)2.9 and Math.Truncate(2.9) both drop the decimals. Truncate returns a double, so it also works for values far bigger than an int can hold.
Subtract the truncated value: double fraction = value - Math.Truncate(value). For 12.75 this gives 0.75.
No. Math is a static class, so you call Math.Truncate(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.