I’m looking for a format specifier scheme for numbers that bears the characteristics of the ubiquitous “YYYY-MM-DD” etc. format specifiers for dates. I’m looking for:
- Easy to process: It shouldn’t take a lot of effort (algorithmic complexity, performance) to format a number.
- (Optional) User-friendly: It is intuitive enough for users to directly work with this syntax.
- Account for thousands separators and decimal places.
Even though my target domain is JavaScript (i.e. formatting Number instances), I prefer a specifier scheme that is standard, intuitive etc. instead of JavaScript-native. And no, I don’t think the printf number format specifiers are intuitive enough to present to users.
The scheme I’ve come up with so far is:
#,###.##
to say:
- The whole part uses groups of 3 digits separated by commas
- Use 2 decimal places and use a period as the delimiter
I’m feeling “meh” about this scheme and don’t want to invest too much time in implementing it before seeing better alternatives.
What I’ve also experiemented with is, to be done with a format specifier and actually use a preferences object with distinct properties for thousands/decimal delimiters, number of decimal places etc. and abstract it by presenting to the user sample number outputs to pick from.
Should I forget about using a format specifier scheme and just use a set of properties that are abstracted at presentation layer? Is there a standard(ish) specifier that meets the 3 criteria I mentioned at the top?
At the end of the day, I’m not without a solution and I have enough to get me going. I’m just looking for a more ideal solution.
XSLT is a standard which has a comprehensive decimal-format scheme.
ECMA-402 is another standard which has a number formatting API:
A more user friendly solution would be something like the NumberFormat or DecimalFormat schemes of CFML.
References
Building world-ready applications in JavaScript using IE11 – IEBlog
Intl.NumberFormat – JavaScript | MDN
Language subtag lookup app
Choosing a language tag
W3C I18N Server Setup Techniques