Padding numbers with zeros in Ruby

String#% method allows to specify format that should be applied to its argument. It can be used to pad numbers with zeros.

Leading zeros

> "%03d" % 2
=> "002"

Trailing zeros

> "%.2f" % 2
=> "2.00"