No need to reinvent the wheel. DecimalFormat comes with currency support:
String output = DecimalFormat.getCurrencyInstance().format(123.45);
This also comes with full locale support by optionally passing in a Locale:
String output = DecimalFormat.getCurrencyInstance(Locale.GERMANY).format( 123.45);
Here's a test:
System.out.println(DecimalFormat.getCurrencyInstance().format( 123.45) );
System.out.println(DecimalFormat.getCurrencyInstance(Locale.GERMANY).format( 123.45)) ;
Output:
$123.45
123,45 €
Answer from Bohemian on Stack OverflowNo need to reinvent the wheel. DecimalFormat comes with currency support:
String output = DecimalFormat.getCurrencyInstance().format(123.45);
This also comes with full locale support by optionally passing in a Locale:
String output = DecimalFormat.getCurrencyInstance(Locale.GERMANY).format( 123.45);
Here's a test:
System.out.println(DecimalFormat.getCurrencyInstance().format( 123.45) );
System.out.println(DecimalFormat.getCurrencyInstance(Locale.GERMANY).format( 123.45)) ;
Output:
$123.45
123,45 €
You can try the following:
public static void main(String[] args) {
System.out.println(String.format(" %d \u20AC", 123)); // %d for integer
System.out.println(String.format(" %.2f \u20AC", 123.10)); // %f for floats
}
This prints:
123 €
123.10 €
Videos
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".
Try using setCurrency on the instance returned by getCurrencyInstance(Locale.GERMANY)
Broken:
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY);
System.out.println(format.format(23));
Output: 23,00 €
Fixed:
java.util.Currency usd = java.util.Currency.getInstance("USD");
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY);
format.setCurrency(usd);
System.out.println(format.format(23));
Output: 23,00 USD
I would add to answer from les2 https://stackoverflow.com/a/7828512/1536382 that I believe the number of fraction digits is not taken from the currency, it must be set manually, otherwise if client (NumberFormat) has JAPAN locale and Currency is EURO or en_US, then the amount is displayed 'a la' Yen', without fraction digits, but this is not as expected since in euro decimals are relevant, also for Japanese ;-).
So les2 example could be improved adding format.setMaximumFractionDigits(usd.getDefaultFractionDigits());, that in that particular case of the example is not relevant but it becomes relevant using a number with decimals and Locale.JAPAN as locale for NumberFormat.
java.util.Currency usd = java.util.Currency.getInstance("USD");
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(
java.util.Locale.JAPAN);
format.setCurrency(usd);
System.out.println(format.format(23.23));
format.setMaximumFractionDigits(usd.getDefaultFractionDigits());
System.out.println(format.format(23.23));
will output:
USD23
USD23.23
In NumberFormat code something similar is done for the initial/default currency of the format, calling method DecimalFormat#adjustForCurrencyDefaultFractionDigits. This operation is not done when the currency is changed afterwards with NumberFormat.setCurrency
Whenever you display a currency, you need two pieces of info: the currency code, and the locale for which you are displaying a result. For example, when you display USD in the en_US locale, you want it to show $, but in the en_AU locale, you want it to show as US$ (because Australia's currency is also called "dollars" and they use the $ symbol for AUD).
The issue you've hit is that the stock Java currency formatters stink when you are displaying a currency in its "non-primary" locale. That is, pretty much everyone in the US would rather see £ and € when showing pounds and euros, but the stock Java libraries show "GBP" and "EUR" when showing those currencies in the en_US locale.
I got around this with the following piece of code. I essentially specify my own symbols to use when displaying international currencies:
public static NumberFormat newCurrencyFormat(Currency currency, Locale displayLocale) {
NumberFormat retVal = NumberFormat.getCurrencyInstance(displayLocale);
retVal.setCurrency(currency);
//The default JDK handles situations well when the currency is the default currency for the locale
if (currency.equals(Currency.getInstance(displayLocale))) {
return retVal;
}
//otherwise we need to "fix things up" when displaying a non-native currency
if (retVal instanceof DecimalFormat) {
DecimalFormat decimalFormat = (DecimalFormat) retVal;
String correctedI18NSymbol = getCorrectedInternationalCurrencySymbol(currency, displayLocale);
if (correctedI18NSymbol != null) {
DecimalFormatSymbols dfs = decimalFormat.getDecimalFormatSymbols(); //this returns a clone of DFS
dfs.setInternationalCurrencySymbol(correctedI18NSymbol);
dfs.setCurrencySymbol(correctedI18NSymbol);
decimalFormat.setDecimalFormatSymbols(dfs);
}
}
return retVal;
}
private static String getCorrectedInternationalCurrencySymbol(Currency currency, Locale displayLocale) {
ResourceBundle i18nSymbolsResourceBundle =
ResourceBundle.getBundle("correctedI18nCurrencySymbols", displayLocale);
if (i18nSymbolsResourceBundle.containsKey(currency.getCurrencyCode())) {
return i18nSymbolsResourceBundle.getString(currency.getCurrencyCode());
} else {
return currency.getCurrencyCode();
}
}
Then I have my properties file (correctedI18nCurrencySymbols.properties) where I specify currency symbols to use:
# Note that these are the currency symbols to use for the specified code when displaying in a DIFFERENT locale than
# the home locale of the currency. This file can be edited as needed. In addition, if in some case one specific locale
# would use a symbol DIFFERENT than the standard international one listed here, then an additional properties file
# can be added making use of the standard ResourceBundle loading algorithm. For example, if we decided we wanted to
# show US dollars as just $ instead of US$ when in the UK, we could create a file i18nCurrencySymbols_en_GB.properties
# with the entry USD=$
ARS=$AR
AUD=AU$
BOB=$b
BRL=R$
CAD=CAN$
CLP=Ch$
COP=COL$
CRC=\u20A1
HRK=kn
CZK=K\u010D
DOP=RD$
XCD=EC$
EUR=\u20AC
GTQ=Q
GYD=G$
HNL=L
HKD=HK$
HUF=Ft
INR=\u20B9
IDR=Rp
ILS=\u20AA
JMD=J$
JPY=JP\u00A5
KRW=\u20A9
NZD=NZ$
NIO=C$
PAB=B/.
PYG=Gs
PEN=S/.
PHP=\u20B1
PLN=\u007A\u0142
RON=lei
SGD=S$
ZAR=R
TWD=NT$
THB=\u0E3F
TTD=TT$
GBP=\u00A3
USD=US$
UYU=$U
VEF=Bs
VND=\u20AB
I am not familiar with Spring, but the following appears to be the issue: Locale.getDefault(). Instead, try using one of the static instances defined on the Locale class.
@artjomka
I was able to reproduce your problem by setting my default locale to Latvia
Locale.setDefault(new Locale("lv","LV"));
Currency c = Currency.getInstance("EUR");
System.out.println(c.getSymbol());
This gave me the output of "EUR".
However, by leaving setting my locale to Uk (already my default) I get the symbol for the Euro(€).
Locale.setDefault(Locale.UK);
Currency c = Currency.getInstance("EUR");
System.out.println(c.getSymbol());
You can use the Currency object's getSymbol method.
What symbol is used depends on the Locale which is used See this and this.
Update, Jan 2016: The links are now dead. But they were specific to Java 1.4/5 so not really relevant anymore. More details on currency formatting can be found in https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html. The links can be found on the WayBackEngine though.
Does it have to use DecimalFormat?
If not, it looks like the following should work:
String currencyString = NumberFormat.getCurrencyInstance().format(currencyNumber);
//Handle the weird exception of formatting whole dollar amounts with no decimal
currencyString = currencyString.replaceAll("\\.00", "");
Use NumberFormat:
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
double doublePayment = 100.13;
String s = n.format(doublePayment);
System.out.println(s);
Also, don't use doubles to represent exact values. If you're using currency values in something like a Monte Carlo method (where the values aren't exact anyways), double is preferred.
See also: Write Java programs to calculate and format currency