tags:

views:

76

answers:

3

hi, i have an int. i want print it in this format: ex.

1 -> 000001
15 -> 000015

How can i do? thanks

+6  A: 
sprintf "%06d", 1     #=> "000001"
sprintf "%06d", 15    #=> "000015"

or more briefly

"%06d" % 1     #=> "000001"
"%06d" % 15    #=> "000015"
Mike Woodhouse
+1  A: 

You can use Kernel#sprintf, or string formatting (%) like so:

>> "%06d" % 1
=> "000001"
>> "%06d" % 15
=> "000015"
maerics
+1  A: 
 "#{1}".rjust(6,'0') # => 000001
"#{15}".rjust(6,'0') # => 000015
Justin L.