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

A lot of people using sprintf (which is the right thing to do), and I think if you want to do this for a string it’s best to keep in mind the rjust and ljust methods:

"4".rjust(2, '0')

This will make the "4" right justified by ensuring it’s at least 2 characters long and pad it with '0'. ljust does the opposite.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)