问题描述
字符串的parseFloat是否限制该字符串可以包含多少个字符?我在这里看不到有关限制的任何内容: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/parseFloat
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?
推荐答案
在Javascript中,浮点数存储为双精度值.这些数字大约有16位有效数字,这意味着不一定要准确地存储17位数字.
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.
您可以为parseFloat()
提供任意长度的数字,但不能存储大于1.79769× 10 的任何数字,这是可以存储的最大值在双精度变量中.
You can supply numbers of any length to parseFloat()
, but it won't be possible to store anything larger than 1.79769×10, 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
这篇关于parseFloat字符串长度超过16个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!