How can I get a NumberFormat (or DecimalFormat) instance corresponding to an ISO 4217 currency code (such as “EUR” or “USD”) in order to format prices correctly?
Note 1: The problem I’m having is that the
NumberFormat/DecimalFormatclasses have a
getCurrencyInstance(Locale locale)method but I can’t figure out how
to get to aLocaleobject from an ISO 4217 currency code.Note 2: There is also a
java.util.Currencyclass which has agetInstance(String currencyCode)method (returning theCurrency
instance for a given ISO 4217 currency code) but again I can’t figure
out how to get from aCurrencyobject to aNumberFormat
instance…
I’m not sure I understood this correctly, but you could try something like:
Output:
Notice that I set the maximum amount of fractional digits separately, the NumberFormat.setCurrency doesn’t touch the maximum amount of fractional digits: