javascript - toFixed covert problem in (444.2).toFixed(14)

70 Views Asked by At

as the title

when I use toFixed to convert the float

(0.2).toFixed(4) ->       0.2000
(444.2).toFixed(4) ->   444.2000
(0.2).toFixed(14) ->      0.20000000000000
(444.2).toFixed(14) ->  444.19999999999999  //why ?!!!

I could not understand that what causes this result.

Is any javascript method to avoid this problem?

1

There are 1 best solutions below

1
On

Floating point numbers cannot represent all decimals precisely in binary. one way to overcome this problem is using parseFloat:

console.log(parseFloat((444.2).toFixed(14)));