If I want "00001" instead of "1", apart from writing my own filling zero method, is there any built in method can help me fill in zero for the integer?
puts "%05d" % 1 # 00001
See: String::%, Kernel::sprintf
Here's what's going on. The "%05d"
to the left of the %
is a C style format specifier. The variable on the right side of the % is the thing to be formatted. The format specifier can be decoded like this:
If you were formatting multiple things, you'd put them in an array:
"%d - %s" % [1, "One"] # => 1 - one
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With