Javascript parseInt 给出了非常意想不到的结果

发布于 2024-09-11 12:50:51 字数 1078 浏览 4 评论 0原文

可能的重复:
JavaScript parseInt 八进制错误的解决方法

我正在解析一个字符串以检查它是否是一个日期,现在我们偶然发现我的方法不适用于八月或九月的日期。这就是我所做的(显然,输入并不是真正硬编码的,但为了简洁......):

var str = '2010-08-26 14:53';
var data = str.split(' ');  // ['2010-08-26', '14:53']
var date = data[0].split('-'); // ['2010', '08', '26]
var time = data[1].split(':'); // ['14', '53']

var yyyy = parseInt(date[0]); // 2010

// THIS IS WHERE STRANGE THINGS HAPPEN:
var MM = parseInt(date[1]); // 0 - not 08 or 8, as expected!
console.log(date[1]); // prints "08" (with quotes)
console.log(date[1].toString()); // prints 08 (no quotes)
console.log(parseInt(date[1].toString())); // prints 0 (!)

这个问题在八月和九月以及每个月的 8 号和 9 号出现 -也就是说,当 "08""09" 被解析为整数时,返回 0 而不是 8或<代码>9。该代码适用于较低的(例如 "07")和较高的(例如 "10")整数(至少在预期的日期范围内......)

我在做什么错误的?

Possible Duplicate:
Workarounds for JavaScript parseInt octal bug

I'm parsing a string to check if it's a date, and by chance we now discovered that my method doesn't work for dates in august or september. This is what I do (the input isn't really hard-coded, obviously, but for brevity...):

var str = '2010-08-26 14:53';
var data = str.split(' ');  // ['2010-08-26', '14:53']
var date = data[0].split('-'); // ['2010', '08', '26]
var time = data[1].split(':'); // ['14', '53']

var yyyy = parseInt(date[0]); // 2010

// THIS IS WHERE STRANGE THINGS HAPPEN:
var MM = parseInt(date[1]); // 0 - not 08 or 8, as expected!
console.log(date[1]); // prints "08" (with quotes)
console.log(date[1].toString()); // prints 08 (no quotes)
console.log(parseInt(date[1].toString())); // prints 0 (!)

This problem arises for august and september, and for the 8th and 9th every month - that is, when either "08" or "09" is being parsed to integer, 0 is returned instead of 8 or 9. The code works for both lower (e.g. "07") and higher (e.g. "10") integers (at least within expected date ranges...)

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

下雨或天晴 2024-09-18 12:50:51

用于

parseInt(date[1], 10)

确保字符串被解释为以 10 为基数(十进制)。

否则,如果以 "0" 开头,则解释为基数 8(八进制);如果以 "0x" 开头,则解释为基数 16(十六进制)。

事实上,您应该始终包含基本参数以避免此类错误。

Use

parseInt(date[1], 10)

to make sure the string is interpreted as base 10 (decimal).

Otherwise, it is interpreted as base 8 (octal) if it starts with "0", or as base 16 (hexadecimal) if it starts with "0x".

In fact, you should always include the base argument to avoid these kinds of bugs.

烟燃烟灭 2024-09-18 12:50:51

这是因为以 0 开头的数字被视为八进制数字,而“08”不是八进制,因此返回 0。

阅读此内容:http://mir.aculo。 us/2010/05/12/adventures-in-javascript-number-parsing/

That's because numbers that start with 0 are treated as octal numbers and "08" is not an octal so 0 is returned.

Read this: http://mir.aculo.us/2010/05/12/adventures-in-javascript-number-parsing/

舞袖。长 2024-09-18 12:50:51

parseInt() 函数有第二个可选基数参数。如果省略 radix 参数,JavaScript 会进行以下假设:

  • 如果字符串以“0x”开头,则基数为 16(十六进制)
  • 如果字符串以“0”开头,则基数为 8(八进制)。此功能已弃用
  • 如果字符串以任何其他值开头,则基数为 10(十进制),

因此在您的情况下,它假定为八进制数。更改为 var MM = parseInt(date[1], 10); 就可以了

The parseInt() function has the second optional radix parameter. 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)

So in your case it assumes the octal numbers. Change to var MM = parseInt(date[1], 10); and it will work

缱绻入梦 2024-09-18 12:50:51

听起来你的日期被解析为十六进制 - 前导零正在执行此操作。

因为 0-7 的十六进制和十进制表示形式是相同的,所以它“有效”,但 8 和 9 的转换不正确。

使用

parseInt(date[1], 10)

显式指定基数。

It sounds like your date is being parsed as hex - the leading zero is doing this.

Because the hex and decimal representations are the same for 0-7 it 'works', but 8 and 9 get incorrectly converted.

Use

parseInt(date[1], 10)

To specify the base explicitly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文