Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
因此,如果这"%05d" % 123返回#=> "00123",我也希望"%05d" % 0123返回#=> "00123",但它会返回#=> "00083"。为什么会这样?
"%05d" % 123
#=> "00123"
"%05d" % 0123
#=> "00083"
以 0 开头的数字被解释为八进制,就像以 0x 开头的数字被解释为十六进制一样。83 用八进制表示为 123。
irb(main):001:0> 0123 => 83 irb(main):002:0> 1*8**2 + 2*8**1 + 3*8**0 => 83 irb(main):003:0> "%05d" % 0x7b => "00123"