Creating a financial application requires you to format number as a currency. It should include the correct thousand separator, decimal separator and the currency symbol. For this purpose you can use the NumberFormat.getCurrencyInstance()
method and pass the correct Locale
to get the currency format that you want.
package org.kodejava.text;
import java.text.NumberFormat;
import java.util.Locale;
public class LocaleCurrencyFormat {
public static void main(String[] args) {
Double number = 1500D;
// Format currency for Canada locale in Canada locale,
// the decimal point symbol is a comma and currency
// symbol is $.
NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CANADA);
String currency = format.format(number);
System.out.println("Currency in Canada : " + currency);
// Format currency for Germany locale in German locale,
// the decimal point symbol is a dot and currency symbol
// is €.
format = NumberFormat.getCurrencyInstance(Locale.GERMANY);
currency = format.format(number);
System.out.println("Currency in Germany: " + currency);
}
}
Here is an output for the currency format using the Locale.CANADA
and Locale.GERMANY
.
Currency in Canada : $1,500.00
Currency in Germany: 1.500,00 €