Formatting integer (2 printed as 02)

Hi,

I want to format an integer as hours and minutes.

Of course it’s easy to write this myself, but let’s respect DRY!

Is there a method I can use so that integers are converted to strings of
2 digits?

So:
2 --> 02
11 --> 11
4 --> 04

thanks!

Yep, you can use sprintf:

sprintf(“%0.2d”, 4)
=> “04”


Thiago J.
acts_as_solr => http://acts-as-solr.rubyforge.org
Sitealizer Web Stats => http://sitealizer.rubyforge.org

On Mar 28, 3:50 pm, Brij N. [email protected]

4 --> 04
sprintf("%02d", 4) => 04

-philip

also,

i=61; “%02i:%02i” % [i / 60, i % 60]
=> “01:01”

On Mar 29, 6:50 am, Brij N. [email protected]