문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top