当一个数字具有前导零时会发生什么? [英] What happens when a number has a leading zero?

查看:78
本文介绍了当一个数字具有前导零时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天一直试图调试问题,最后我发现了问题所在。出于某种原因,当一个数字具有前导零时,JavaScript会做一些非常奇怪的事情。

I've been trying to debug a problem today and I finally found out what the problem is. For some reason, when a number has a leading zero JavaScript does something really weird.

示例:

alert(132);
alert(0132);

var test = 0132;
alert(test);

JSFiddle: http://jsfiddle.net/U8sFu/3/

JSFiddle: http://jsfiddle.net/U8sFu/3/

第一个弹出窗口显示132,第二个90,第三个说90。

The first popup says "132," the second "90," and the third says "90."

我的问题是,为什么数字0132成为数字90?这对我来说真是莫名其妙!

My question is, why does the number 0132 become the number 90? This is really baffling to me!

推荐答案

带前导0的数字被解析为octal literal ,位于8号基地。

A number with a leading 0 is parsed as an octal literal, which is in base 8.

这篇关于当一个数字具有前导零时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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