How can I get the currency-formatted amount on Android without the country qualifier?

20 Views Asked by At

I'm formatting currency values using NumberFormat.getCurrencyInstance and Currency.getInstance which works well to determine things like whether the currency symbol should go before or after the number based on locale, and use of dots vs commas.

However, for certain locale/currency combinations it prints out a currency with the country-specific qualifier. This seems to be where the currency originates from a single locale, and the locale specified is not the one from which the currency originates - but not always. For example:

private String getFormattedCurrency(String language, String country, String currency, BigDecimal value) {
    var locale = new Locale(language, country);
    var nf = NumberFormat.getCurrencyInstance(locale);
    nf.setCurrency(Currency.getInstance(currency));
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);
    return nf.format(value);
}

public void test() {
    var bd = new BigDecimal ("12.34");
    var x = getFormattedCurrency("FR", "FR", "GBP", bd); // x = "12,34 £GB", presumably to differentiate between British pounds and Egyptian pounds
    var y = getFormattedCurrency("EN", "GB", "USD", bd); // y = "US$12.34"
    var z = getFormattedCurrency("EN", "CA", "CAD", bd); // z = "$12.34", same locale where the currency originates from
    var zz = getFormattedCurrency("EN", "GB", "EUR", bd); // zz = "€12.34", currency does not originate from a single locale
    var zzz = getFormattedCurrency("EL", "GR", "GBP", bd); // zzz = "12,34 £", no idea why this doesn't have the country-specific qualifier whereas x above does have it (Greece is closer to Egypt than France is!)
}

Is there a way to get the formatted value without the country-specific qualifier (e.g. without "GB"/"US" above)? (Aside from just setting the locale to the same one where the currency originates, which does cause the country-specific qualifier to be removed, but at the expense of properly formatting the currency string according to the actual desired locale.)

0

There are 0 best solutions below