开发者

Why does 0154 === 108? [duplicate]

Th开发者_运维技巧is question already has answers here: Number with leading zero in JavaScript (3 answers) Closed 5 years ago.

What's happening here and why?

document.write(0154); // === 108


Numbers that begin with 0 are considered octal (base-8) numbers.

base 8 [0154] = base 10 [108]

but if you had used a number that had an 8 or 9 you wouldn't have seen this problem since that neither 8 nor 9 is an octal digit.


0154 is octal. 1*64 + 5*8 + 4 = 108.


its octal number. octal=0154 & decimal is=108


It is printing out the octal equivalent of what you wrote because it started with 0. Try 0001 (prints out 1), 0010 (prints out 8), 0011 (prints out 9)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜