parseInt()问题 [英] parseInt() question

查看:55
本文介绍了parseInt()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么parseInt(" 0000000000000018")返回1,而

parseInt(&0000000000000018",10)返回18?


我的假设是基数10将是

基数的默认参数。难道你不想在大多数情况下回到18岁吗?


有什么想法吗?


-Thx

Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix. Wouldn''t you want to get back 18 in most if not all cases?

Any thoughts?

-Thx

推荐答案

于2007年6月17日在comp.lang.javascript中写道
wrote on 17 jun 2007 in comp.lang.javascript:

为什么parseInt(" 0000000000000018")返回1,而

parseInt(&0000000000000018",10)返回18?


我的假设是基数10将是

基数的默认参数。
Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix.



你的假设是错误的,它是八进制的。

阅读规格:


parseInt (numString,[radix])


numString

必填。要转换为数字的字符串。


radix

可选。介于2和36之间的值,表示numString中包含的数字

的基数。如果没有提供,前缀为''0x''的字符串将被视为十六进制,而前缀为'0'的字符串则被视为
octal。所有其他字符串都被视为十进制。


-

Evertjan。

荷兰。

(请将x''es更改为我的电子邮件地址中的点)

Your assumption is wrong, it is octal.
Read the specs:

parseInt(numString, [radix])

numString
Required. A string to convert into a number.

radix
Optional. A value between 2 and 36 indicating the base of the number
contained in numString. If not supplied, strings with a prefix of ''0x'' are
considered hexadecimal and strings with a prefix of ''0'' are considered
octal. All other strings are considered decimal.

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


ki*******@gmail.com 写道:

为什么parseInt(" 0000000000000018")返回1,而

parseInt(" 0000000000000018",10)返回18?


我的假设是基数10将是

radix的默认参数。如果不是所有情况,你不想在大多数情况下取回18吗?
Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix. Wouldn''t you want to get back 18 in most if not all cases?



parseInt规范中出错。这就是为什么你的b $ b应该总是明确地指出基数。不要依赖于默认值

10.正如您所演示的那样,它不可靠。


JSLint将读取您的来源并确定其中的位置缺少默认值。

http://www.JSLint.com/


Evertjan。写道:
Evertjan. wrote:

于2007年6月17日写在comp.lang.javascript

>为什么parseInt(" 0000000000000018")返回1,而
parseInt(&0000000000000018",10)返回18?

我的假设是基数10会是
基数的默认参数。
>Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix.



你的假设是错误的,它是八进制的。

阅读规格:


parseInt (numString,[radix])


numString

必填。要转换为数字的字符串。


radix

可选。介于2和36之间的值,表示numString中包含的数字

的基数。如果没有提供,前缀为''0x''的字符串将被视为十六进制,而前缀为'0'的字符串则被视为
octal。所有其他字符串都被视为十进制


Your assumption is wrong, it is octal.
Read the specs:

parseInt(numString, [radix])

numString
Required. A string to convert into a number.

radix
Optional. A value between 2 and 36 indicating the base of the number
contained in numString. If not supplied, strings with a prefix of ''0x'' are
considered hexadecimal and strings with a prefix of ''0'' are considered
octal. All other strings are considered decimal.



我想知道你从哪里读到的。核心JavaScript 1.5

明确指出,该行为已被弃用。

http://developer.mozilla.org/en/docs...nt#Description


-

-Lost

删除多余的单词以通过电子邮件回复。不要给我发电子邮件。我是开玩笑的。不,我不是。

I''d like to know where you read that from. The Core JavaScript 1.5
specifically states that, that behavior is deprecated.

http://developer.mozilla.org/en/docs...nt#Description

--
-Lost
Remove the extra words to reply by e-mail. Don''t e-mail me. I am
kidding. No I am not.


这篇关于parseInt()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆