hi, i have an int. i want print it in this format: ex.
1 -> 000001
15 -> 000015
How can i do? thanks
hi, i have an int. i want print it in this format: ex.
1 -> 000001
15 -> 000015
How can i do? thanks
sprintf "%06d", 1 #=> "000001"
sprintf "%06d", 15 #=> "000015"
or more briefly
"%06d" % 1 #=> "000001"
"%06d" % 15 #=> "000015"
You can use Kernel#sprintf, or string formatting (%
) like so:
>> "%06d" % 1
=> "000001"
>> "%06d" % 15
=> "000015"
"#{1}".rjust(6,'0') # => 000001
"#{15}".rjust(6,'0') # => 000015