Do you want to work on this issue?
You can request for a bounty in order to promote it!
Rounding negative number with fractional portion being exactly 0.5 #29
yvele posted onGitHub
roundTo
must be consistent with Math.round
when dealing with fractional portion being exactly 0.5:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round#description
If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞.
This differs from many languages'
round()
functions, which often round half-increments away from zero, giving a different result in the case of negative numbers with a fractional part of exactly 0.5.
// With negative numbers
Math.round(-1.5); // -1
Math.round(-1.51); // -2
// With positive numbers
Math.round(1.5); // 2
I suggest that this should be the behavior a JavaScript developer should expect:
t.is(roundTo(0.5, 0), 1);
- t.is(roundTo(-0.5, 0), -1);
+ t.is(roundTo(-0.5, 0), 0); // Note that we intentionally DON'T have -0 here
t.is(roundTo(1.005, 2), 1.01);
- t.is(roundTo(-1.005, 2), -1.01);
+ t.is(roundTo(-1.005, 2), -1);
Previous discussions:
- https://github.com/sindresorhus/round-to/issues/6
- https://github.com/sindresorhus/round-to/pull/8
- https://github.com/sindresorhus/round-to/issues/28 (about NOT returning values rounded to -0)
Note also that's how Lodash works:
See also https://en.wikipedia.org/wiki/Rounding#Comparison_of_approaches_for_rounding_to_an_integer
I think the exact description is "Half Up (toward +∞)".