9906

3
+45  Q:

## What's the difference between Math.Floor() and Math.Truncate() in .NET?

What is the difference between `Math.Floor()` and `Math.Truncate()` in C#?

+111  A:

`Math.Floor` rounds down, `Math.Ceiling` rounds up, and `Math.Truncate` rounds towards zero. Thus, `Math.Truncate` is like `Math.Floor` for positive numbers, and like `Math.Ceiling` for negative numbers. Here's the reference.

For completeness, `Math.Round` rounds to the nearest integer. If the number is exactly midway between two integers, then it rounds towards the even one. Reference.

@Chris, I suggest you fix your description of Round, there's two ways to round (AwayFromZero and ToEven) and it doesn't round to the nearest *integer* since it can do fractional rounding as well.
+1 for Pax's comment
Thanks for the comment. Since you've provided a much more comprehensive answer, I should perhaps link to yours. :-)
So just a short add on to the original question -what is the difference between Math.Truncate and just casting a decimal or double to int? wouldn't it also just round towards zero?
@Pax Diablo: Given you've changed your nickname back, I'm rolling back your change. :-P
+9  A:

Some examples:

``````Round(1.5) = 2
Round(2.5) = 2
Round(1.5, MidpointRounding.AwayFromZero) = 2
Round(2.5, MidpointRounding.AwayFromZero) = 3
Round(1.55, 1) = 1.6
Round(1.65, 1) = 1.6
Round(1.55, 1, MidpointRounding.AwayFromZero) = 1.6
Round(1.65, 1, MidpointRounding.AwayFromZero) = 1.7

Truncate(2.10) = 2
Truncate(2.00) = 2
Truncate(1.90) = 1
Truncate(1.80) = 1
``````
+59  A:

• `Math.Floor`, which rounds down towards negative infinity.
• `Math.Ceiling`, which rounds up towards positive infinity.
• `Math.Truncate`, which rounds up or down towards zero.
• `Math.Round`, which rounds to the nearest integer or specified number of decimal places. You can specify the behavior if it's exactly equidistant between two possibilities, such as rounding so that the final digit is even ("`Round(2.5,MidpointRounding.ToEven)`" becoming 2) or so that it's further away from zero ("`Round(2.5,MidpointRounding.AwayFromZero)`" becoming 3).

The following diagram and table may help:

``````-3        -2        -1         0         1         2
+--|------+---------+----|----+--|------+----|--|-+
a                     b       c           d  e

a=-2.7  b=-0.5  c=0.3  d=1.5  e=2.8
======  ======  =====  =====  =====
Floor                    -3      -1      0      1      2
Ceiling                  -2       0      1      2      3
Truncate                 -2       0      0      1      2
Round (ToEven)           -3       0      0      2      3
Round (AwayFromZero)     -3      -1      0      2      3
``````

Note that `Round` is a lot more powerful than it seems, simply because it can round to a specific number of decimal places. All the others round to zero decimals always. For example:

``````n = 3.145;
a = System.Math.Round (n, 2, MidpointRounding.ToEven);       // 3.14
b = System.Math.Round (n, 2, MidpointRounding.AwayFromZero); // 3.15
``````

With the other functions, you have to use multiply/divide trickery to achieve the same effect:

``````c = System.Math.Truncate (n * 100) / 100;                    // 3.14
d = System.Math.Ceiling (n * 100) / 100;                     // 3.15
``````
Pax, I think you've got a mistake with:Round(AwayFromZero) -3 -2 1 2 3Math.Round(-1.2, MidpointRounding.AwayFromZero) == -1Math.Round(0.3, MidpointRounding.AwayFromZero)==0.0etc..
Thanks, @dtroy, I've never had a need to use that mode and, while I documented it correctly if the text, I totally got the examples wrong. Hopefully that's fixed now.
awesome post. love ascii art :)