ParseFloat strings longer than 16 characters

Does parseFloat string have a limit on the number of characters in a string? I see nothing about the limit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

But running the next in the console seems to show results that I did not expect.

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

Can anyone break this for me?

+1
source share
1 answer

In Javascript, floating point numbers are stored as double precision values . They have about 16 significant digits, which means that a 17-digit number will not necessarily be accurately stored.

You can specify numbers of any length up to parseFloat() , but cannot store more than 1.79769 & times; 10 308 which is the largest possible value that can be stored in a double precision variable.

I would recommend reading this if you have the time: What every computer scientist needs to know about floating point arithmetic

+2
source

All Articles