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.
我不明白为什么会这样:
s = "000301" "%06d" % s ====> "000193" sprintf("%06d", s) ====> "000193"
因为它被解释为八进制数。
在 irb 中尝试:
> 0301 => 193
但是当你写:
> 301 => 301
如果您想让它工作,请尝试将其转换为整数String#to_i:
String#to_i
"%06d" % s.to_i sprintf("%06d", s.to_i)