Returns a NumberFormat for formatting and parsing currency values for the user's default locale. See "".
Popular methods of NumberFormat
Popular in Java
Many of the answers had helpful ideas, but none of them could fit my needs. So I used all the ideas and build this example:
First, I needed a C-style format-string-definition that should be flexible, but very easy to use and I defined it in following way; patterns:
Because there isn't any need to format others than to euro for me, I implemented only "%m".
But it's easy to extend this... Like in C, the format string is a string containing the patterns. For example, for euro: "%m €" (returns strings like "8.129,33 €")
Besides the flexibility, I needed a very fast solution for processing tables. That means that, when processing thousands of cells, the processing of format string must not be done more than once. A call like "format( value, fmt)" is not acceptable for me, but this must be split into two steps:
Also for performance, _f_money encloses the regular expression;
Third, a call like "format( value, fmt)" is not acceptable because:
Although it should be possible to format different collections of objects (for example, cells of a column) with different masks, I don't want to have something to handle format strings at the point of processing. At this point I only want to use formatting, like in
What format - maybe it's defined in an .ini file, in an XML for each column or somewhere else ..., but analyzing and setting formats or dealing with internationalizaton is processed in totally another place, and there I want to assign the formatter to the collection without thinking about performance issues:
Fourth, I wanted an "tolerant" solution, so passing, for example, a string instead of a number should return simply the string, but "null" should return en empty string.
(Also formatting "%4.2f" must not cut something if the value is too big.)
And last, but not least - it should be readable and easy extendable, without having any effects in performance... For example, if somebody needs "octal values", please refer to lines with "...can be extended..." - I think that should be a very easy task.
My overall focus lay on performance. Each "processing routine" (for example,
For all, who prefer methods of numbers:
The code contains many and little modified ideas from a lot of Stack Overflow posts; sorry I can't reference all of them, but thanks to all the experts.
NumberFormat() Constructor to Format Numbers as Currency. You can use the Intl. NumberFormat() constructor to create Intl. NumberFormat objects that enable language-sensitive number formatting, such as currency formatting.
A stack overflow occurs when there is a recursive function (a function that calls itself) without an exit point. The browser (hosting environment) has a maximum stack call that it can accomodate before throwing a stack error.