Jacek Kwiecień
Jacek Kwiecień

Reputation: 12637

Is NumberFormat.getCurrencyInstance() failing to return right symbol in some countries?

I develop finance app that should use different currency symbol which depends on the country. I'm using this code:

Float value = 100;
TextView tv = (TextView) view;
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
tv.setText(currencyFormat.format(value));

I have several reports that my app is returning Euro symbol instead the right currency (in Malysia or India for example). Those users state that their system language is native to their country. I can't prove that myself because there are no such languages avalaible on my phone or emulator.

Upvotes: 1

Views: 1928

Answers (1)

shri046
shri046

Reputation: 1198

I suppose you could try and force a locale in your app since this seems to be related to how the default locale is being set on the device. See these links -

how to choose serbain language from android Emulator settings?

https://web.archive.org/web/20210127121431/http://www.tutorialforandroid.com/2009/01/force-localize-application-on-android.html

This would probably help you replicate the issue that users reported on their devices.

EDIT: I have not tested this myself but it could work in your case.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String languageToLoad  = "en";
    String countryToLoad = "MY"; // 2 digit country code for Malaysia
    Locale locale = new Locale(languageToLoad, countryToLoad); 
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, 
    getBaseContext().getResources().getDisplayMetrics());
    this.setContentView(R.layout.main);
}

Upvotes: 1

Related Questions