常见问题主题 - 为什么parseInt('09')会出错? [英] FAQ Topic - Why does parseInt('09') give an error?

查看:77
本文介绍了常见问题主题 - 为什么parseInt('09')会出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

----------------------------------------------- ------------------------

常见问题解答主题 - 为什么parseInt(''09'')会出错?

------------------------------------------- ----------------------------


parseInt函数决定数字的基数在这个数字上寻找

。按照惯例,它假定以0x开头的任何数字是
是十六进制的,否则任何以

0开头的数字都是八进制数。强制使用基数10添加第二个参数

``parseInt(" 09",10)'''

http://msdn.microsoft.com/library/en...thparseint.asp

http://docs.sun.com/source/816-6408-...ev.htm#1064173
http://www.jibbering.com/faq/faq_not....html#FAQN4_12

===

这样的发布每天自动发送一次。他们的b
目标是回答重复的问题,并将内容提供给社区进行持续评估/改进。完整的

comp.lang.javascript常见问题解答位于 http:/ /www.jibbering.com/faq/

FAQ工作人员是一群志愿者。

解决方案

JRS:文章< 44 *********************** @ news.sunsite.dk>,日期为周四,
2006年8月24日23:00:01远程,在新闻中看到:comp.lang.javascript,常见问题

服务器< ja ******** @ dotinternet.beposted :


> ------------------------------- ----------------------------------------
常见问题解答主题 - 为什么选择parseInt (''09'')给出错误?
----------------------------------- ------------------------------------

parseInt函数决定了什么基础数字是看数字的。按照惯例,它假设以0x开头的任何数字都是十六进制,否则任何以
0开头的数字都是八进制。强制使用基数10添加第二个参数
``parseInt(" 09",10)''''



......或者使用+09 。



恕我直言,parseInt只应在以下情况中至少使用其中一种时使用: -

基数既不是10,也不是16 by 0x

基数是可变的

字符串可能有尾随的非空格

字符串可能为空,以使NaN不为0。


特别是,它不应该用于匹配/ ^ \s * \d + \\\ *


/


这可能是不完整或次优的 - 想想例如

一个空字符串。

-

?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?

< URL:http://www.jibbering.com/faq/>? JL / RC:新闻常见问题:comp.lang.javascript

< URL:http://www.merlyn.demon.co.uk/js-index.htmjscr数学,日期,来源。

< URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/& c,常见问题项目,链接。

John Stockton博士在8/25/2006上午8:13发表以下内容:


JRS:文章< 44 ** *********************@news.sunsite.dk> ;,日期为星期四,

2006年8月24日23:00:01在新闻中看到:comp.lang.javascript,常见问题

服务器< ja ******** @ dotinternet.beposted:


> ---------------------------------------------- -------------------------
FAQ主题 - 为什么parseInt(''''''出错?
-------------------------------------------------- ---------------------

parseInt函数通过查看数字来确定数字的基数。按照惯例,它假设以0x开头的任何数字都是十六进制,否则任何以
0开头的数字都是八进制。强制使用基数10添加第二个参数
``parseInt(" 09",10)''''



......或者使用+09 。



恕我直言,parseInt只应在以下情况中至少使用其中一种时使用: -

基数既不是10,也不是16 by 0x



为什么要排除16号基数而不是8号基数?


-

兰迪

comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /


-----------------------------------------------------------------------
FAQ Topic - Why does parseInt(''09'') give an error?
-----------------------------------------------------------------------

The parseInt function decides what base the number is by looking
at the number. By convention it assumes that any number beginning
with 0x is Hexadecimal, and otherwise any number beginning with
0 is Octal. To force use of base 10 add a second parameter
`` parseInt("09",10) ''''

http://msdn.microsoft.com/library/en...thparseint.asp

http://docs.sun.com/source/816-6408-...ev.htm#1064173

http://www.jibbering.com/faq/faq_not....html#FAQN4_12
===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

解决方案

JRS: In article <44***********************@news.sunsite.dk>, dated Thu,
24 Aug 2006 23:00:01 remote, seen in news:comp.lang.javascript, FAQ
server <ja********@dotinternet.beposted :

>-----------------------------------------------------------------------
FAQ Topic - Why does parseInt(''09'') give an error?
-----------------------------------------------------------------------

The parseInt function decides what base the number is by looking
at the number. By convention it assumes that any number beginning
with 0x is Hexadecimal, and otherwise any number beginning with
0 is Octal. To force use of base 10 add a second parameter
`` parseInt("09",10) ''''

.... or use +"09" .


IMHO, parseInt should be used only when at least one of these applies :-
The base is neither 10, nor 16 indicated by 0x
The base is variable
The string may have trailing non-whitespace
The string may be empty, to give NaN not 0.

In particular, it should not be used for a match to /^\s*\d+\s*


/

That may be incomplete or sub-optimal - think about exceptions such as
an empty string.
--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.


Dr John Stockton said the following on 8/25/2006 8:13 AM:

JRS: In article <44***********************@news.sunsite.dk>, dated Thu,
24 Aug 2006 23:00:01 remote, seen in news:comp.lang.javascript, FAQ
server <ja********@dotinternet.beposted :

>-----------------------------------------------------------------------
FAQ Topic - Why does parseInt(''09'') give an error?
-----------------------------------------------------------------------

The parseInt function decides what base the number is by looking
at the number. By convention it assumes that any number beginning
with 0x is Hexadecimal, and otherwise any number beginning with
0 is Octal. To force use of base 10 add a second parameter
`` parseInt("09",10) ''''


.... or use +"09" .


IMHO, parseInt should be used only when at least one of these applies :-
The base is neither 10, nor 16 indicated by 0x

Why exclude base 16 but not base 8?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


这篇关于常见问题主题 - 为什么parseInt('09')会出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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