JavaScriptIntermediate#numbers

Why is 0.1 + 0.2 !== 0.3?

Numbers are IEEE-754 doubles, so many decimals cannot be represented exactly and tiny rounding errors appear. Compare with a small epsilon or work in integer minor units for money.

Example
0.1 + 0.2;                       // 0.30000000000000004
Math.abs(0.1 + 0.2 - 0.3) < 1e-9; // true
(0.1*100 + 0.2*100) / 100;        // 0.3

Related Questions

1
JavaScriptIntermediate#dom#events

What is event delegation useful for in dynamic lists?

Open
2
JavaScriptIntermediate#browser#performance

What is the difference between defer and async on a script tag?

Open
3
JavaScriptAdvanced#performance#memory

What causes memory leaks in JavaScript?

Open