parseFloat of string longer than 16 characters

不问归期 提交于 2020-01-05 07:35:30

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/19613256/parsefloat-of-string-longer-than-16-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!