+ 3

Why 0.1 + 0.2 = 0.30000000000000004 (in Javascript)?

0.1 + 0.2 = 0.3, but in Javascript 0.30000000000000004. Why?

2nd Jul 2017, 6:28 AM
V13
V13 - avatar
1 Odpowiedź
+ 5
Computer can't store floating point too accurately. Use toFixed to set it to less decimal point. In equelity comparison, never apply strict equality to floating point "===", use equality "==" and adjust toFixed function instead. console.log((0.1+ 0.2)===0.3? "true":"false"); // return false console.log((0.1+ 0.2).toFixed(16)==0.3? "true":"false"); // return true
2nd Jul 2017, 7:00 AM
Calviղ
Calviղ - avatar