DEV Community

Isha Saxena
Isha Saxena

Posted on

Why 0.1 +0.2 is not equal to 0.3 is javaScript ???

Its because 0.1+0.2 will not give you 0.3 expected
Because numbers in JS are stored in binary format and it uses
IEEE-754 format to store the number

Here the CATCH ISSSS if the number size is longer than 64 bit then the least significant figures are rounded up

The solution is to use toPrecision() this method allows to cut or round up the remaining disturbing noise in this calculation for eg

if say let sum=0.1 +0.2
Image description
let result=parseFloat(sum.toPrecision(12))
now console.log(result===0.3)

javascriptinterviewquestions

Top comments (0)