Reputation: 60681
I need to be able to display currency amounts where the currency is of the user's preferred locale, but using only Western digits. For example, if the user's chosen locale for the currency is ar_AE
(for the United Arab Emirates) then the amount should be displayed as
AED 1,234.56
or something similar, using the correct currency name and number of decimal places, but using Western digits.
At the moment, I'm using NumberFormat.getCurrencyInstance(locale)
which formats the text using Arabic glyphs.
What options do I have? I'm looking for a general solution which won't require me to have specific knowledge of any given currency.
Upvotes: 0
Views: 1509
Reputation: 34313
If you want a currency formatter with number formatting according to the English locale, but using the default locale's currency, I would try something like this:
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.ENGLISH);
nf.setCurrency(Currency.getInstance(Locale.getDefault()));
Upvotes: 3
Reputation: 88707
You might use DecimalFormat
like this:
System.out.println(new DecimalFormat( "AED #0.00" , new DecimalFormatSymbols( Locale.ENGLISH)).format( 1.23 ));
Edit: to use the locale for currency too, try this:
System.out.println(Currency.getInstance( Locale.GERMANY ).getCurrencyCode() + " " +
new DecimalFormat( "#0.00" , new DecimalFormatSymbols( Locale.GERMANY )).format( 1.23 ));
Prints: EUR 1,23
Upvotes: 1