Ruby on Rails: How do you add add zeros in front of a number if it's under 10?

Posted by sjsc on Stack Overflow See other posts from Stack Overflow or by sjsc
Published on 2010-04-22T16:58:41Z Indexed on 2010/04/22 17:03 UTC
Read the original article Hit count: 241

Filed under:
|

I'm looking to convert single digit numbers to two-digit numbers like so:

9 ==> 09
5 ==> 05
12 == 12
4 ==> 04

I figure I could put a bunch of if-else statements (if number is under 10, then do a gsub) but figure that's horrible coding. I know Rails has number_with_precision but I see that it only applies to decimal numbers. Any ideas on how to convert single-digits to two-digits?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails