As you know in ruby you can do
"%03d" % 5
#=>  "005"
"%03d" % 55
#=> "055"
"%03d" % 555
#=> "555"
so basically number will have "0" prefix for 3 string places
just wondering is there possibility to do number string suffix in similar nice way ? (please no if statements)
something 5
#=> 500
something 55
#=> 550
something 555
# => 555
                how about ljust method?
"5".ljust(3, "0")
and some to_s and to_i method calls if you want to do that to integers
you could avoid string conversion with bit more math like log_10 to find number of digits in an integer and then i *= 10**x where x is how many more 0's you need
like this:
def something(int, power=3)
  int * 10**([power - Math.log10(int).to_i - 1, 0].max)
end
                        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