tags:

views:

157

answers:

3

Is it possible to custom format currency strings using the ICU library similar to the way it lets you format time strings by providing a format string (e.g. "mm/dd/yyy").

So that for a given locale (say USD), if I wanted I could have all currency strings come back "xxx.00 $ USD".

A: 

Use the ICU library's createCurrencyInstance().

Shmoopty
I believe that only returns the currency format for a given locale that is embedded in that locale. So for USD it would return something like "$100.00 USD". I'm looking for a way to customize that format however I want. So if I wanted "[value][symbol] [currency name]" even though that's not how that locale is typically displayed, I could get it.
Travis
+1  A: 

See http://icu-project.org/apiref/icu4c/classDecimalFormat.html,

Specifically: http://icu-project.org/apiref/icu4c/classDecimalFormat.html#aadc21eab2ef6252f25eada5440e3c65

For pattern syntax see: http://icu-project.org/apiref/icu4c/classDecimalFormat.html#_details

I didn't used this but from my knowledge of ICU this is the direction.

However I would suggest to use:

http://icu-project.org/apiref/icu4c/classNumberFormat.html and createCurrencyInstance member and then use setMaximumIngegerDigits or other functions to make what you need -- that would be much more localized. Try not assume anything about any culture. Because "10,000 USD" my be misinterpreted as "$ 10" in some countries where "," used for fraction part separation.

So be careful.

Artyom
+1  A: 

You can create a currency instance, then if it is safe to cast it to a DecimalFormat

if (((const NumberFormat*)fmt)->getDynamicClassID() == DecimalFormat::getStaticClassID()) 
  {   const DecimalFormat* df = (const DecimalFormat*) fmt; ...

… then you can call applyPattern on it. See the information on ¤, ¤¤, ¤¤¤ under 'special pattern chars'

Steven R. Loomis