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

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.

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.

See also: Pax Diablo's answer.

8 @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. – paxdiablo Feb 24 '09 at 2:44 Thanks for the comment. Since you've provided a much more comprehensive answer, I should perhaps link to yours.

:-) – Chris Jester-Young Apr 11 '09 at 22:05 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?

– Noam Gal May 19 '09 at 10:40 @Pax Diablo: Given you've changed your nickname back, I'm rolling back your change. :-P – Chris Jester-Young Oct 14 '09 at 2:08.

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 be 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 be = 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.

2 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.. – dtroy May 5 '09 at 3:49 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.

– paxdiablo May 5 '09 at 4:16 2 awesome post. Love ascii art :) – Pure. Krome May 22 '09 at 7:21 Like the chart to show exactly what each one is doing.

Simple elegant reference. – SaulBack Nov 2 '11 at 16:28.

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(1.55) = 2 Truncate(1.55) = 1 Truncate(1.55) = 1.

Math.Floor() rounds down the number while Math. Truncate rounds towards zero e.g. Math. Floor(-3.4) = -4 Math.

Truncate(-3.4) = -3.

E.g. Math. Floor(5.4) = 5 Math.

Truncate(5.4) = 5.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions