JavaScript 中 0.3 的文字表示

Literal representation of 0.3 in JavaScript

如果我在 Chrome 控制台中输入 0.3,它会向我返回 0.3

这基本上是在说:"you typed in a Number literal with the contents 0.3, and I replay that representation back to you in the console as a convenience, even though under the hood and in reality it cannot be exactly represented and the best approximation I can come up with is 0.30000000000000004"?

实际上,internal representation

0.2999999999999999988897769753748434595763683319091796875

它本可以打印 0.29999999999999999,这是四舍五入到 17 位有效数字的内部值。四舍五入为 17 digits is the conservative way to preserve any internal value。但在这种情况下 0.3 也能正常工作(毕竟,这就是你开始的地方),而且因为它更短,所以打印出来的就是它。

所以它没有接受您的字符串输入并将其回显——它就是这样解决的(对于任何 15 位或更少有效数字的输入都是如此)。