[X]

A $1.005 Price Rounds Down

luljs still works

Round a price of 1.005 to two decimals and it becomes 1.00, not 1.01.

In 2026: Still works, and it is the float problem in disguise. 1.005 cannot be stored exactly; the nearest double is a hair below it, so toFixed rounds down to "1.00". Any money code built on toFixed inherits this, which is why currency is better handled in whole cents.

Where it came from: 1.005 has no exact binary representation, so the stored value is just under it and toFixed rounds down. A corollary of IEEE 754 doubles. MDN

<script>
document.write('<p><code>(1.005).toFixed(2)</code> is <b>"' + (1.005).toFixed(2) + '"</b></p>');
document.write('<p>The value really stored: <code>(1.005).toFixed(20)</code> is <b>' + (1.005).toFixed(20) + '</b></p>');
document.write('<p>It sits just under 1.005, so it rounds down.</p>');
</script>
sandboxed demo · breaks nothing but itselfrestart

More in luljs

all 63 in luljs › · the whole library ›