0 to number

I want to print it in this format: ex.

1 -> 000001
15 -> 000015

How can i do this? thank

+3
source share
3 answers
sprintf "%06d", 1     #=> "000001"
sprintf "%06d", 15    #=> "000015"

or more briefly

"%06d" % 1     #=> "000001"
"%06d" % 15    #=> "000015"
+9
source
 "#{1}".rjust(6,'0') # => 000001
"#{15}".rjust(6,'0') # => 000015
+3
source

You can use Kernel # sprintf or line formatting ( %) as follows:

>> "%06d" % 1
=> "000001"
>> "%06d" % 15
=> "000015"
+1
source

Source: https://habr.com/ru/post/1750635/


All Articles