Search code examples
androidlocalizationlocale

How to format a Float Value with the device currency format?


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?


Solution

  • 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));