26
loading...
This website collects cookies to deliver better user experience
I'll use several notions of rounding: banker's rounding, away from zero and round half up. More info about different techniques can be found on Wikipedia.
Math.Round(0.5); // 0
Math.Round(1.5); // 2
Math.Round(2.5); // 2
Math.Round(3.5); // 4
Math.Round(-23.5); // -24
Math.Round(0.5, MidpointRounding.AwayFromZero); // 1
Math.Round(1.5, MidpointRounding.AwayFromZero); // 2
Math.Round(2.5, MidpointRounding.AwayFromZero); // 3
Math.Round(3.5, MidpointRounding.AwayFromZero); // 4
Math.Round(-23.5, MidpointRounding.AwayFromZero); // -24
Math.Round(0.5, MidpointRounding.ToPositiveInfinity); // 1
Math.Round(1.5, MidpointRounding.ToPositiveInfinity); // 2
Math.Round(2.5, MidpointRounding.ToPositiveInfinity); // 3
Math.Round(3.5, MidpointRounding.ToPositiveInfinity); // 4
Math.Round(-23.5, MidpointRounding.ToPositiveInfinity); // -23
Math.round(0.5); // 1
Math.round(1.5); // 2
Math.round(2.5); // 3
Math.round(3.5); // 4
Math.round(-23.5); // -23
round(0.5) # 1
round(1.5) # 2
round(2.5) # 3
round(3.5) # 4
round(-23.5) # -24
round(0.5) # 0
round(1.5) # 2
round(2.5) # 2
round(3.5) # 4
round(-23.5) # -24
This was quite surprising, to be honest.
Math.round(0.5); // 1
Math.round(1.5); // 2
Math.round(2.5); // 3
Math.round(3.5); // 4
Math.round(-23.5); // -23
math.Round(0.5) // 1
math.Round(1.5) // 2
math.Round(2.5) // 3
math.Round(3.5) // 4
math.Round(-23.5) // -24
math.RoundToEven(0.5) // 0
math.RoundToEven(1.5) // 2
math.RoundToEven(2.5) // 2
math.RoundToEven(3.5) // 4
math.RoundToEven(-23.5) // -24
round(0.5); # 1
round(1.5); # 2
round(2.5); # 3
round(3.5); # 4
round(-23.5); # -24