以“0"开头的数字文字 [英] Numeric literals prepended with `0`
本文介绍了以“0"开头的数字文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 insert
,我将值推送到 Array
为:
Using insert
, I push values to an Array
as:
myarray=[22,33,44]
myarray.insert(0,02)
# => [2,22,33,44]
如果执行以下操作,我会得到:
If do the following, I get:
myarray.insert(0,020)
# => [16,2,22,33,44]
020
变成 16
?如果我执行以下操作,我会得到:
020
becomes 16
? If I do the following, I get:
myarray.insert(0,0200)
# => [128,16,2,22,33,44]
0200
变成 128
?我可以知道这是什么原因吗?
0200
becomes 128
? May I know the reason for this?
推荐答案
如果数字前面有零,ruby 会将其视为八进制数(基数为 8)
If the number has a zero in front of it, ruby treats it as an octal number (base 8)
你也可以用二进制/十六进制做类似的事情
You can do similar with binary/hexadecimal too
0x20 => 32 (hexadecimal)
020 => 16 (octal)
0b10 => 2 (binary)
080 => Invalid octal digit
这篇关于以“0"开头的数字文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文