I was just googling to see if there was any information on presenting
numeric fields with leading zeros, but have found no discussion either
here or in the wiki.
In my app, I would like to display leading zeros on id fields that are
used as external user references. eg. customer.id or order.id etc -
both in forms and in table/text entries. To give a fixed sized number
eg. 5 digits.
Does anyone have any clever solutions. One way to do it would be to
use a method call such as:
pad(ref_id,5) etc.
The other approach is to put an entry in the model to provide the
ref_id as a padded string.
I am using MySQL which allows id fields to be zero filled, but this
doesnt seem to help, and also there is the lpad(id,0,5) function, but I
am not sure it would make sense to try and use this from within rails.
I just wondered if there is anything already built into rails that I am
overlooking, or if there is a better approach - Any thoughts?
I was just googling to see if there was any information on presenting
numeric fields with leading zeros, but have found no discussion either
here or in the wiki.
I just use string formatting - e.g.
“%05d” % an_int
if an_int = 123 it will give you the string “00123”
I can use it, but I have just realised I dont really know how it works!
I have been looking at % in ruby but cannot relate it to this
solution?. I would really appreciate a pointer.