JavaScript 中 parseFloat 的第二个参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8555649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Second argument to parseFloat in JavaScript?
提问by AmbroseChapel
In this font-size resizing tutorial:
在这个字体大小调整教程中:
the author uses parseFloat with a second argument, which I read here:
作者使用 parseFloat 和第二个参数,我在这里读到:
Is supposed to specify the base of the supplied number-as-string, so that you can feed it '0x10' and have it recognized as HEX by putting 16 as the second argument.
应该指定所提供数字作为字符串的基数,以便您可以将其输入“0x10”并通过将 16 作为第二个参数将其识别为十六进制。
The thing is, no browser I've tested seems to do this.
问题是,我测试过的浏览器似乎没有这样做。
Are these guys getting confused with Java?
这些人是否对 Java 感到困惑?
回答by Ry-
No, they're getting confused with parseInt()
, which can take a radix parameter. parseFloat()
, on the other hand, only accepts decimals. It might just be for consistency, as you should always pass a radix parameter to parseInt()
because it can treat numbers like 010
as octal, giving 8
rather than the correct 10
.
不,他们会混淆parseInt()
,它可以采用基数参数。parseFloat()
,另一方面,只接受小数。这可能只是为了一致性,因为您应该始终将基数参数传递给,parseInt()
因为它可以将数字010
视为八进制,给出8
而不是正确的10
.