ruby位数自动补齐

作者: 张飞_
发布时间:2015-07-13 11:31:53

case:1

max = 0
b=["1","3","100","1000"]
b.each{|key|
  len = key.to_s.length
  (len>max)?(max=len):()
}

b.each{|key|
  puts "0"*(max - key.to_s.length)+key.to_s
}

 

case:2

15.times do |i|
  puts sprintf("%02d\n",i)
end

标签: Ruby
来源:http://www.cnblogs.com/zhangfei/archive/2012/08/17/2644345.h

推荐: