Another approach :
NumberFormat format = NumberFormat.getCurrencyInstance();
format.setMaximumFractionDigits(0);
format.setCurrency(Currency.getInstance("EUR"));
format.format(1000000);
This way, it's displaying 1 000 000 € or 1,000,000 €, depending on device currency's display settings
Another approach :
NumberFormat format = NumberFormat.getCurrencyInstance();
format.setMaximumFractionDigits(0);
format.setCurrency(Currency.getInstance("EUR"));
format.format(1000000);
This way, it's displaying 1 000 000 € or 1,000,000 €, depending on device currency's display settings
You need to use a number formatter, like so:
NumberFormat formatter = new DecimalFormat("#,###");
double myNumber = 1000000;
String formattedNumber = formatter.format(myNumber);
//formattedNumber is equal to 1,000,000
Hope this helps!
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.89with US locale1 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.
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));