1

Does parseFloat of a string have a limit to how many characters the string can be? I don't see anything about a limit here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

But running the following in console seems to show results I wasn't expecting.

parseFloat('1111111111111111'); // 16 characters long
// result 1111111111111111

parseFloat('11111111111111111'); // 17 characters long
// result 11111111111111112

Can anyone break this down for me?

edward.louis
  • 357
  • 2
  • 5
  • 14

1 Answers1

2

In Javascript, floating point numbers are stored as double precision values. These have about 16 significant digits, which means that a 17-digit number won't necessarily be stored exactly.

You can supply numbers of any length to parseFloat(), but it won't be possible to store anything larger than 1.79769×10308, which is the largest possible value that can be stored in a double precision variable.

I'd recommend reading this if you have time: What Every Computer Scientist Should Know About Floating-Point Arithmetic

r3mainer
  • 23,981
  • 3
  • 51
  • 88