What is the difference between Math.Floor() and Math.Truncate() in C#?
-
Math.Floorrounds down,Math.Ceilingrounds up, andMath.Truncaterounds towards zero. Thus,Math.Truncateis likeMath.Floorfor positive numbers, and likeMath.Ceilingfor negative numbers. Here's the reference.For completeness,
Math.Roundrounds to the nearest integer. If the number is exactly midway between two integers, then it rounds towards the even one. Reference.See also: Pax Diablo's answer. Highly recommended!
paxdiablo : @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.Ant : +1 for Pax's commentChris Jester-Young : Thanks for the comment. Since you've provided a much more comprehensive answer, I should perhaps link to yours. :-)Noam Gal : 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?Chris Jester-Young : @Pax Diablo: Given you've changed your nickname back, I'm rolling back your change. :-P -
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 -
Follow these links for the MSDN descriptions of:
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 3Note that
Roundis 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.15With 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.15dtroy : Pax, I think you've got a mistake with: Round(AwayFromZero) -3 -2 1 2 3 Math.Round(-1.2, MidpointRounding.AwayFromZero) == -1 Math.Round(0.3, MidpointRounding.AwayFromZero)==0.0 etc..paxdiablo : 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.Pure.Krome : awesome post. love ascii art :)
0 comments:
Post a Comment