I am in a situation where a JavaScript function produces numbers, such as 2.5
. I want to have these point five numbers rounded down to 2
, rather than the result of Math.round
, which will always round up in such cases (ignoring the even odd rule), producing 2. Is there any more elegant way of doing this than subtracting 0.01 from the number before rounding? Thanks.

- 143
- 1
- 5
-
6If you're doing "banking" math, it's a fundamental mistake to use any binary floating-point system. – Pointy Mar 06 '16 at 00:59
-
1If you are trying to do a bankers rounding this might help: http://stackoverflow.com/questions/3108986/gaussian-bankers-rounding-in-javascript – drew_w Mar 06 '16 at 01:03
4 Answers
Just negate the input and the output to Math.round
:
var result = -Math.round(-num);
In more detail: JavaScript's Math.round
has the unusual property that it rounds halfway cases towards positive infinity, regardless of whether they're positive or negative. So for example 2.5
will round to 3.0
, but -2.5
will round to -2.0
. This is an uncommon rounding mode: it's much more common to round halfway cases either away from zero (so -2.5
would round to -3.0
), or to the nearest even integer.
However, it does have the nice property that it's trivial to adapt it to round halfway cases towards negative infinity instead: if that's what you want, then all you have to do is negate both the input and the output:
Example:
function RoundHalfDown(num) {
return -Math.round(-num);
}
document.write("1.5 rounds to ", RoundHalfDown(1.5), "<br>");
document.write("2.5 rounds to ", RoundHalfDown(2.5), "<br>");
document.write("2.4 rounds to ", RoundHalfDown(2.4), "<br>");
document.write("2.6 rounds to ", RoundHalfDown(2.6), "<br>");
document.write("-2.5 rounds to ", RoundHalfDown(-2.5), "<br>");

- 29,088
- 9
- 83
- 120
-
I don't think this solution still works anymore. Can someone else check as well? – CptKicks Oct 08 '19 at 09:32
-
The "Run code snippet" button still produces the results I expect (Safari, macOS). Can you elaborate on what part isn't working for you? – Mark Dickinson Oct 08 '19 at 09:36
do this:
var result = (num - Math.Floor(num)) > 0.5 ? Math.Round(num):Math.Floor(num);

- 412
- 3
- 11
-
This is not what the OP is looking for. They want a function that will determine whether to round up or down automatically based on the value. – jfriend00 Mar 06 '16 at 01:10
Another way exists that is to use real numbers (instead of 0.2 use 20, 0.02 use 2, etc.), then add floatingPoints variable that will divide the result (in your case it's 2). As a result you can operate as Number/(10^floatingPoints). This solution is wide across Forex companies.

- 56
- 2
-
There's still a (good) chance for error when doing division operations. – Pointy Mar 06 '16 at 01:07
You can also use this function to round with no decimal part and .5 down rule (Only positive numbers):
function customRound(number) {
var decimalPart = number % 1;
if (decimalPart === 0.5)
return number - decimalPart;
else
return Math.round(number);
}
And sorry for my english.

- 41
- 3