The problem is that you are using a static method NumberFormat.getCurrencyInstance() that works that way:
public final static NumberFormat getCurrencyInstance() {
return getInstance(Locale.getDefault(Locale.Category.FORMAT), CURRENCYSTYLE);
}
As you can see, it returns a NumberFormat object with a default Locale, not the Locale you are setting. That means it depends on user device settings.
So, instead of calling format on that statically returned instance with default Locale:
nf.getCurrencyInstance().format(parsed / 100);
which is effectively the same as:
NumberFormat.getCurrencyInstance().format(parsed / 100);
Use your instance with Locale that you've set:
final NumberFormat nf = NumberFormat.getInstance(Locale.US);
String formatted = nf.format(parsed / 100);
Answer from mkierc on Stack OverflowAnother 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!
Videos
You can try some like this:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Utils.getCurrencySymbol(Currency.getInstance(Locale.US).getCurrencyCode());
Utils.getCurrencySymbol(Currency.getInstance(Locale.JAPAN).getCurrencyCode());
Utils.getCurrencySymbol(Currency.getInstance(Locale.UK).getCurrencyCode());
//for your case that you want to get Euro symbol because France are with Euro symnol
Utils.getCurrencySymbol(Currency.getInstance(Locale.FRANCE).getCurrencyCode());
//you can get symbol also if you write string of your desired currency
Utils.getCurrencySymbol("INR");
}
static class Utils {
public static SortedMap<Currency, Locale> currencyLocaleMap;
static {
currencyLocaleMap = new TreeMap<Currency, Locale>(new Comparator<Currency>() {
public int compare(Currency c1, Currency c2) {
return c1.getCurrencyCode().compareTo(c2.getCurrencyCode());
}
});
for (Locale locale : Locale.getAvailableLocales()) {
try {
Currency currency = Currency.getInstance(locale);
currencyLocaleMap.put(currency, locale);
} catch (Exception e) {
}
}
}
public static String getCurrencySymbol(String currencyCode) {
Currency currency = Currency.getInstance(currencyCode);
System.out.println(currencyCode + ":-" + currency.getSymbol(currencyLocaleMap.get(currency)));
return currency.getSymbol(currencyLocaleMap.get(currency));
}
}
}
Currency symbol depends on location. The same $ sign means different currencies in US and Australia. So, to get the correct symbol you have to provide the Locale instance. Otherwise, a default locale will be applied, which will result in different values for different devices.
Locale uk = new Locale("en", "GB");
Currency pound = Currency.getInstance("GBP");
pound.getSymbol(uk);
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));
Different currencies can also place the currency symbol before or after the string, or have a different number of decimal places (if any). It is not really clear from your question how you want to handle those cases, but assuming you want to preserve those differences, try this.
Instead of just swapping in the currency symbol into your local number format, you could start with the foreign format and substitute the decimal format symbols with your local version. Those also include the currency, so you have to swap that back (don't worry, it's a copy).
public static NumberFormat localStyleForeignFormat(Locale locale) {
NumberFormat format = NumberFormat.getCurrencyInstance(locale);
if (format instanceof DecimalFormat) {
DecimalFormat df = (DecimalFormat) format;
// use local/default decimal symbols with original currency symbol
DecimalFormatSymbols dfs = new DecimalFormat().getDecimalFormatSymbols();
dfs.setCurrency(df.getCurrency());
df.setDecimalFormatSymbols(dfs);
}
return format;
}
This way, you also retain the correct positioning of the currency symbol and the number of decimal places. Some examples, for default-Locale Locale.UK
en_GB £500,000.00 £500,000.00
fr_FR 500 000,00 € 500,000.00 €
it_IT € 500.000,00 € 500,000.00
ja_JP ¥500,000 JPY500,000
hi_IN रू ५००,०००.०० INR 500,000.00
If you also want to preserve the foreign currency symbol, instead of the local equivalent, use
localDfs.setCurrencySymbol(df.getCurrency().getSymbol(locale));
You can specify the currency symbol on the NumberFormat with the setCurrency method.
Then simply use the Locale.UK to have the proper grouping separator displayed.
format.setCurrency(Currency.getInstance("EUR"));
Note that for a better handling of the grouping/decimal separator you might want to use a DecimalFormat instead.
DecimalFormatSymbols custom=new DecimalFormatSymbols();
custom.setDecimalSeparator('.');
custom.setGroupingSeparator(',');
DecimalFormat format = DecimalFormat.getInstance();
format.setDecimalFormatSymbols(custom);
format.setCurrency(Currency.getInstance("EUR"));
Then specify the correct pattern, example "€ ###,###.00".
Locale locale = Locale.getDefault();
Currency currency = Currency.getInstance(locale);
String symbol = currency.getSymbol().replaceAll("\\w", "");
try this, Hope it works.
yourString= NumberFormat.getCurrencyInstance().format(yourNumber);
or
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
String cur = currencyFormatter.format(yourValue);
for specific
yourString= NumberFormat.getCurrencyInstance(new Locale("en", "AU")).format(yourNumber);
number will format based on device language.
Detail:
How to get currency symbol by currency name?
Currency