1

I tried a lot.But was unable to find a solution. I have a code for formatting currency. I am using the below code :

NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);

Here I am facing an issue. Consider a case with France locale. In my case, locale can be en_FR and fr_FR.

DecimalFormatSymbols decimalFormats = new DecimalFormatSymbols();
decimalFormats.setCurrencySymbol(currencySymbol);
((DecimalFormat) numberFormat).setDecimalFormatSymbols(decimalFormats);

formattedCurrency = numberFormat.format(Double.valueOf(number));

So if the locale is en_FR, the formattedCurrency value will be € 10.00 and if the locale is fr_FR, the value will be 10.00 €.

So I would like to know the role of language code in this calculation methods.Since en_FR has en, I guess it is by default taking as en_US. So currency symbol coming left of price.

I need to get 10.00 € always if the country is France irrespective of language code. Is there are other way to get the currency formatting based on country, rather than locale?

achAmháin
  • 4,176
  • 4
  • 17
  • 40
Jince Martin
  • 211
  • 1
  • 3
  • 14

2 Answers2

2

Just specify the country code , instead of language and country

try to modify like below for you :

String countryCode="FR"; // or use "String countryCode=Locale.getDefault().getCountry();" for system default locale
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(new Locale(countryCode));

Note:

The Locale points to language and not to country specific , above code will work as expected in most scenario where the ISO country code and language code will be the same.

FYI

Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
  • 1
    Except, the single-argument constructor of Locale takes a **language** not a country! It's defined as `public Locale(String language)`. This answer is very incorrect, and it only works because the language code `fr` is the same as the country code `FR`. But that doesn't go for other countries - take Korea - country code `KR`, language code for Korean is `ko`. – Erwin Bolwidt Oct 23 '17 at 14:36
  • @erwin I tried this code with korea. It worked. May be I am not clear on what your are telling. Please clear me out – Jince Martin Oct 24 '17 at 10:00
  • As Erwin said, the first argument for the Locale constructor is language, not country. Actually even in the cases of countries where language and country codes are the same, this won't work. You need to specify both language and country. – andresp Feb 19 '21 at 17:05
0

You can simply specify FRANCE as your Locale, and it will always print it on the right hand side for you.

String formattedCurrency = DecimalFormat.getCurrencyInstance(Locale.FRANCE).format(10.00);

Output:

10,00 €

Edit:

You can get the user's Locale:

Locale currentLocale = Locale.getDefault();

And pass that as the parameter:

String formattedCurrency = DecimalFormat.getCurrencyInstance(currentLocale).format(10.00);

I'm not in France, but for me it will print:

€10.00

Which is correct for Ireland.

2nd Edit:

You can get the country name from the currentLocale above, by getting the display country - but this is stored as a String

String country = currentLocale.getDisplayCountry();

Then convert it to a Locale to use as a parameter

public static Locale getLocaleFromString(String localeString) {
        return new Locale(localeString, "");
    }

Note: this method is an edited snippet from here

And use it in your formatter then

Locale l = getLocaleFromString(country);
String number = DecimalFormat.getCurrencyInstance(l).format(10.00);
achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • France is just a country. It can be any country. I took France as an example. I am getting locale details from cookie. So it should be generic. – Jince Martin Oct 23 '17 at 12:57
  • I guess you are not getting my point. I am trying on your updated code.If the currentLocale is en_FR, you will get €10.00 and if it is fr_FR you will get 10.00 €. Right? My query is that can i get 10.00 € in both cases, for France irrespective of language code. Both users are in France only. – Jince Martin Oct 23 '17 at 13:22
  • Is there any way other than locale to find currency format. Like based on country code or anything like that – Jince Martin Oct 23 '17 at 13:24