In our application we have a string of digits (stored as a String) for a registration number (Australian Business Number or Australian Company Number, similar to a SSN), for example:
"98123123999"
When we display this information in our web application it would be nice to be able to reformat the digits to be more readable, e.g.:
98 123 123 999
In ruby the best solution I have so far is to:
- Check the length
- Format the string by using the slice operator:
[a[0,2], a[2,3], a[5,3], a[8, 3]].join(" ")
However this smells and doesn't seem right.
Whats the best way to do something seemingly so simple?
Edit: I should note as well that the string of numbers could be prefixed with a 0 (e.g. "012345678901") so it isn't possible to parse it as an integer then use format strings.