You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 numbersMath.round(-1.5);// -1Math.round(-1.51);// -2// With positive numbersMath.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);
roundTo
must be consistent withMath.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
I suggest that this should be the behavior a JavaScript developer should expect:
Previous discussions:
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 +∞)".
The text was updated successfully, but these errors were encountered: