56

I have an app that prints a calculated money value and I want to display that value with the default currency format.

For example in Europe you would write:

1.000,95€

In the US I think you would write

1,000.95$

In other currencies there are more or less values displayed for the decimal fraction, in US it would be 2 but in Japan it would be 0.

How can I obtain a exact as possible format for all existing currencies?

Janusz
  • 187,060
  • 113
  • 301
  • 369

4 Answers4

88

For sake of completeness suppose you want to display a price in phone's current locale (correct decimal mark and thousand separator) but with a currency of your choice.

NumberFormat format = NumberFormat.getCurrencyInstance(Locale.getDefault());
format.setCurrency(Currency.getInstance("CZK"));
String result = format.format(1234567.89);

result would then hold these values:

  • CZK1,234,567.89 with US locale
  • 1 234 567,89 Kč with Czech locale

If you'd like to omit the decimal part for prices (show $199 instead of $199.00) call this before using the formatter:

format.setMinimumFractionDigits(0);

All options are listed in NumberFormat docs.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
  • This link helpful for Locale http://stackoverflow.com/questions/4212320/get-the-current-language-in-device?rq=1 – Arpit Patel Sep 20 '16 at 13:39
  • In case somebody has issues in getting correct currency symbols, here an alternative https://stackoverflow.com/questions/9777689/how-to-get-numberformat-instance-from-currency-code – Junior Mayhé Jun 10 '17 at 21:19
79

I found the solution. THe class NumberFormat has a multitude of predefined formatters. There is also one for formatting currency Values.

If you use the static method getCurrencyInstance the class will return a formatter for the device default currency. I use the following code to set my result:

NumberFormat format = NumberFormat.getCurrencyInstance();
((TextView) findViewById(R.id.text_result)).setText(format.format(result));
Janusz
  • 187,060
  • 113
  • 301
  • 369
  • 1
    what will happen in the case result = 1000, and your phone locale is FR, but you are in the US? Will it write 1 000 € instead of $1,000? The currency format is not going to account for conversion or result correct? – paul_hundal Jan 26 '17 at 23:02
  • @paul_hundal it depends on your device's locale/language, and it does not matter the (physical) location – Luis Cabrera Benito Feb 06 '19 at 23:00
3

Check out DecimalFormat.

It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, or Indic digits. It also supports different flavors of numbers, including integers ("123"), fixed-point numbers ("123.4"), scientific notation ("1.23E4"), percentages ("12%"), and currency amounts ("$123"). All of these flavors can be easily localized.

2

to get device locale you could use this:

@TargetApi(Build.VERSION_CODES.N)
    public static Locale getCurrentLocale(Context c) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return c.getResources().getConfiguration().getLocales().get(0);
        } else {
            //noinspection deprecation
            return c.getResources().getConfiguration().locale;
        }

and then to do format:

        double num = 1323.526;

        NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(getCurrentLocale(getContext()));
        defaultFormat.format(num)
  • 1
    `locale` is equal to `getLocales().get(0)`. Or there's [ConfigurationCompat#getLocales](https://developer.android.com/reference/android/support/v4/os/ConfigurationCompat). There's no need to do this yourself. – Eugen Pechanec May 01 '18 at 22:37