javascript“parseInt()”的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7318385/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:44:03  来源:igfitidea点击:

Problems with javascript "parseInt()"

javascript

提问by joedborg

Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
How to parseInt a string with leading 0

可能的重复:
JavaScript parseInt 八进制错误的解决方法
How to parseInt a string with leading 0

document.write(parseInt("07"));

Produces "7"

产生“7”

document.write(parseInt("08"));

Produces "0"

产生“0”

This is producing problems for me (sorry for this babble, I have to or I can't submit the question). Anyone know why it's being stupid or if there is a better function?

这给我带来了问题(抱歉这个胡言乱语,我必须或我无法提交问题)。任何人都知道为什么它很愚蠢或者是否有更好的功能?

回答by Dennis

If you argument begins with 0, it will be parsed as octal, and 08 is not a valid octal number. Provide a second argument 10which specifies the radix - a base 10 number.

如果参数以 0 开头,它将被解析为八进制,并且 08 不是有效的八进制数。提供10指定基数的第二个参数- 基数为 10 的数字。

document.write(parseInt("08", 10));

回答by Marek Sebera

use this modification

使用这个修改

parseInt("08",10);

rules for parseInt(string, radix)

规则 parseInt(string, radix)

If the radix parameter is omitted, JavaScript assumes the following:

如果省略 radix 参数,JavaScript 假定如下:

  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with "0", the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)
  • 如果字符串以“0x”开头,则基数为16(十六进制)
  • 如果字符串以“0”开头,则基数为 8(八进制)。此功能已弃用
  • 如果字符串以任何其他值开头,则基数为 10(十进制)

回答by Quentin

You want parseInt('08', 10)to tell it to parse as a decimal.

你想parseInt('08', 10)告诉它解析为十进制。

回答by chaZm

You just using input parameters of that function in a bit wrong way. Check thisfor more info. Basically :

您只是以一种有点错误的方式使用了该函数的输入参数。检查这个以获取更多信息。基本上 :

The parseInt() function parses a string and returns an integer.

The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following: If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal)

parseInt() 函数解析一个字符串并返回一个整数。

radix 参数用于指定使用哪种数字系统,例如基数为 16(十六进制)表示字符串中的数字应从十六进制数解析为十进制数。

如果省略 radix 参数,JavaScript 假定如下: 如果字符串以“0x”开头,则基数为 16(十六进制) 如果字符串以“0”开头,则基数为 8(八进制)。此功能已弃用 如果字符串以任何其他值开头,则基数为 10(十进制)

So 07 and 08 is parsed into octal . Thats why 07 is 7 and 08 is 0 (it is rounded to closest)

所以 07 和 08 被解析为八进制。这就是为什么 07 是 7 而 08 是 0(四舍五入到最接近的)

回答by Tushar Ahirrao

Try this :

试试这个 :

parseInt('08', 10)

it will produce 8

它将产生 8