NumberFormat has the following constructors:
- format a number with:
- "NumberFormat.getInstance()"
- or "NumberFormat.getInstance(Locale)"
- or "NumberFormat.getNumberInstance()"
- or "NumberFormat.getNumberInstance(Locale)",
- format a currency with:
- "NumberFormat.getCurrencyInstance()"
- or "NumberFormat.getCurrencyInstance(Locale)",
- format a percentage with:
- "NumberFormat.getPercentInstance()"
- or "NumberFormat.getPercentInstance(Locale)",
- to see more constructors: http://docs.oracle.com/javase/8/docs/api/java/text/NumberFormat.html
Eg.
// We set the Locale's language to "en" and country to "US" (English used in Us)
Locale locale = new Locale("en", "US");
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);
NumberFormat percentFormat = NumberFormat.getPercentInstance(locale);
double number = 10.50;
double currency = 10.50;
double percent = 10.50;
System.out.println("Number format: "+numberFormat.format(number));
System.out.println("Currency format: "+currencyFormat.format(currency));
System.out.println("Percent format: "+percentFormat.format(percent));
Output:
Number format: 10.5
Currency format: $10.50
Percent format: 1,050%
In the previous example if we set the Locale to:
Locale locale = new Locale("fr", "FR");
The output will be:
Number format: 10,5
Currency format: 10,50 €
Percent format: 1 050 %