热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何处理JavaScript的parseIntoctal行为?-HowdoIworkaroundJavaScript'sparseIntoctalbehavior?

TryexecutingthefollowinginJavaScript:尝试在JavaScript中执行以下操作:parseInt(01);equals1parseIn

Try executing the following in Javascript:

尝试在Javascript中执行以下操作:

parseInt('01'); //equals 1
parseInt('02'); //equals 2
parseInt('03'); //equals 3
parseInt('04'); //equals 4
parseInt('05'); //equals 5
parseInt('06'); //equals 6
parseInt('07'); //equals 7
parseInt('08'); //equals 0 !!
parseInt('09'); //equals 0 !!

I just learned the hard way that Javascript thinks the leading zero indicates an octal integer, and since there is no "8" or "9" in base-8, the function returns zero. Like it or not, this is by design.

我刚学到了Javascript认为前导零表示八进制整数,因为在base-8中没有“8”或“9”,函数返回零。不管你喜不喜欢,这都是由设计决定的。

What are the workarounds?

解决方法是什么?

Note: For sake of completeness, I'm about to post a solution, but it's a solution that I hate, so please post other/better answers.

注意:为了完整性起见,我将发布一个解决方案,但它是我讨厌的解决方案,所以请发布其他/更好的答案。


Update:

更新:

The 5th Edition of the Javascript standard (ECMA-262) introduces a breaking change that eliminates this behavior. Mozilla has a good write-up.

第5版的Javascript标准(ECMA-262)引入了一个可以消除这种行为的中断更改。Mozilla有一个不错的记录。

9 个解决方案

#1


316  

This is a common Javascript gotcha with a simple solution:

这是一个常见的Javascript问题,有一个简单的解决方案:

Just specify the base, or 'radix', like so:

只要指定基数,或者“基数”,就像这样:

parseInt('08',10); // 8

You could also use Number:

你也可以用数字:

Number('08'); // 8

#2


42  

If you know your value will be in the signed 32 bit integer range, then ~~x will do the correct thing in all scenarios.

如果您知道您的值将在已签名的32位整数范围内,那么~~x将在所有场景中做正确的事情。

~~"08" === 8
~~"foobar" === 0
~~(1.99) === 1
~~(-1.99)  === -1

If you look up binary not (~), the spec requires a "ToInt32" conversion for the argument which does the obvious conversion to an Int32 and is specified to coerce NaN values to zero.

如果你查找二进制文件而不是(~),该规范要求对参数进行“ToInt32”转换,该转换将明显转换为Int32,并指定将NaN值强制为0。

Yes, this is incredibly hackish but is so convenient...

是的,这是令人难以置信的黑客,但是非常方便……

#3


24  

From the parseInt documentation, use the optional radix argument to specify base-10:

从parseInt文档中,使用可选的radix参数指定base-10:

parseInt('08', 10); //equals 8
parseInt('09', 10); //equals 9

This strikes me as pedantic, confusing, and verbose (really, an extra argument in every single parseInt?) so I'm hoping there is a Better Way.

这让我觉得学究式的、混乱的和冗长的(实际上,在每个parseInt中都有一个额外的参数?),所以我希望有更好的方法。

#4


11  

function parseDecimal(s) { return parseInt(s, 10); }

edit: making your own function, to do what you really want, is just an option if you don't like adding the ",10" all the time to the parseInt() call. It has the disadvantage of being a nonstandard function: more convenient for you if you use it a lot, but perhaps more confusing for others.

编辑:让你自己的功能,做你真正想做的,只是一个选项,如果你不喜欢添加“,10”一直到parseInt()调用。它有一个缺点,它是一个非标准的功能:如果你经常使用它,它会更方便,但是对其他人来说可能更容易混淆。

#5


10  

Specify the base:

指定的基础:

var number = parseInt(s, 10);

#6


7  

Would it be very naughty to replace parseInt with a version that assumes decimal if it has no second parameter? (note - not tested)

如果将parseInt替换为一个没有第二个参数的版本,它会非常顽皮吗?(注意,不是测试)

parseIntImpl = parseInt
parseInt = function(str, base){return parseIntImpl(str, base ? base : 10)}

#7


5  

How about this for decimal:

小数部分呢?

('09'-0) === 9  // true

('009'-0) === 9 // true

#8


4  

If you've done a bunch of coding already with parseInt and don't want to add ",10" to everything, you can just override the function to make base 10 the default:

如果你已经用parseInt做了一堆编码,并且不想添加“,10”,你可以重写这个函数以使基底10为默认值:

window._oldParseInt = window.parseInt;
window.parseInt = function(str, rad) {
    if (! rad) {
        return _oldParseInt(str, 10);
    }
    return _oldParseInt(str, rad);
};

That may confuse a later reader, so making a parseInt10() function might be more self-explanatory. Personally I prefer using a simple function than having to add ",10" all the time - just creates more opportunity for mistakes.

这可能会使后面的读者感到困惑,所以做一个parseInt10()函数可能更容易理解。就我个人而言,我更喜欢使用简单的函数,而不是每次都要添加“,10”,只会给错误带来更多的机会。

#9


3  

You may also, instead of using parseFloat or parseInt, use the unary operator +.

您也可以使用unary运算符+,而不是使用parseFloat或parseInt。

+"01"
// => 1
+"02"
// => 2
+"03"
// => 3
+"04"
// => 4
+"05"
// => 5
+"06"
// => 6
+"07"
// => 7
+"08"
// => 8
+"09"
// => 9

and for good measure

和另外

+"09.09"
// => 9.09

MDN Link

MDN链接

The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number.

一元加运算符先于它的操作数,并计算它的操作数,但如果它还没有,则尝试将它转换成一个数字。虽然unary negation(-)也可以转换非数字,但unary plus是将某些东西转换成数字的最快和首选方法,因为它不执行任何其他操作。


推荐阅读
author-avatar
ekuuu
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有