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

let result=parseFloat(sum.toPrecision(12))
now console.log(result===0.3)
javascriptinterviewquestions
Top comments (0)