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?
Did you mean
sprintf '%02d', n?You might want to reference the format table for
sprintfin the future, but for this particular example'%02d'means to print an integer (d) taking up at least 2 characters (2) and left-padding with zeros instead of spaces (0).