问题
I tried to make manual currency. Here is my code
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("$");
dfs.setGroupingSeparator('.');
dfs.setDecimalSeparator('.');
df.setDecimalFormatSymbols(dfs);
System.out.println(df.format(3333454));
Program output is
3.333.454
Why the currency symbol I set didn't appear?
回答1:
Try this:
NumberFormat df = NumberFormat.getCurrencyInstance();
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("$");
dfs.setGroupingSeparator('.');
dfs.setMonetaryDecimalSeparator('.');
((DecimalFormat) df).setDecimalFormatSymbols(dfs);
System.out.println(df.format(3333454));
回答2:
Because you use the DecimalFormat with the standard pattern. You need to provide your custom pattern with the \u00A4
currency symbol.
Or you use NumberFormat.getCurrencyInstance().
回答3:
You've told the DecimalFormat which currency symbol to use when it must format a currency. But you haven't told it to format a currency. The default pattern used by the no-arg constructor isn't meant to format currencies. Use a dedicated pattern for that.
The javadoc tells you everything you need to know.
来源:https://stackoverflow.com/questions/10536899/how-to-set-customize-currency-in-java