0

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?

Ayush
  • 457
  • 8
  • 16
Dana Chen
  • 216
  • 3
  • 14

1 Answers1

1

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)));
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
  • Mmm... Actually, I need to use the `toFixed(14)` to display the price like `444.20000000000000`. Thanks for your response. :D – Dana Chen Jun 02 '21 at 03:18