parseInt为什么返回NAN为“08”字符串并返回7为“07”串? [英] Why does parseInt return NAN for "08" string and return 7 for "07" string?

查看:83
本文介绍了parseInt为什么返回NAN为“08”字符串并返回7为“07”串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的应用程序脚本,如下所示:

  function testeBug(){
Logger.log(parseInt ( 07));
Logger.log(parseInt(08));
}

以下是记录器输出:

[13-06-19 23:09:13:130 BRT] 7.0
[13-06-19 23:09:13:130 BRT] NaN



为什么会发生这种情况?
我使用Google Apps脚本

解决方案

您需要将radix参数传递给parseInt

  parseInt(08,10); 

如果不这样做,会导致某些浏览器将前导零的字符串视为base-8,即你所看到的,因为基地-8中的07是7,而08是无效的。


I created a simple apps script as follows:

function testeBug() {
  Logger.log(parseInt("07"));
  Logger.log(parseInt("08"));
}

And here's the logger output:

[13-06-19 23:09:13:130 BRT] 7.0 [13-06-19 23:09:13:130 BRT] NaN

Why this is happening? I'm using Google Apps Script

解决方案

You need to pass in the radix parameter to parseInt

parseInt("08", 10);

Failure to do so causes some browsers to treat strings with a leading zero as base-8, which is what you're seeing, since 07 in base-8 is 7, while 08 is invalid.

这篇关于parseInt为什么返回NAN为“08”字符串并返回7为“07”串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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