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

Janusz picture Janusz · Aug 20, 2011 · Viewed 55.7k times · Source

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?

Answer

Eugen Pechanec picture Eugen Pechanec · May 8, 2016

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.