How can I format integers in a text_field so that they always consist of 6 digits?
So for example when the integer is stored as 1 in the database, in the text_field it would say: 000001. The user should also be able to edit these values, yet not reduce or exceed the number of digits.
I tried something like this but it’s not working for me:
def formatted_number(int)
int.to_s.rjust(6, '0')
end
Thanks for any help.
Use string formatting:
For the sake of readability you can also use the explicit call to
Kernel#format:Refer to the
Kernel#formatdocumentation for the syntax of the format string.