如何在java中设置自定义货币?

时间:2012-05-10 15:05:33

标签: java format set currency

我试图制作手动货币。这是我的代码

DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("$");
dfs.setGroupingSeparator('.');
dfs.setDecimalSeparator('.');
df.setDecimalFormatSymbols(dfs);
System.out.println(df.format(3333454));

程序输出

  

3.333.454

为什么我设置的货币符号没有出现?

3 个答案:

答案 0 :(得分:25)

试试这个:

NumberFormat df = NumberFormat.getCurrencyInstance();
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("$");
dfs.setGroupingSeparator('.');
dfs.setMonetaryDecimalSeparator('.');
((DecimalFormat) df).setDecimalFormatSymbols(dfs);
System.out.println(df.format(3333454));

答案 1 :(得分:4)

因为您使用标准模式的DecimalFormat。您需要使用\u00A4货币符号提供自定义模式。

或者您使用NumberFormat.getCurrencyInstance()

答案 2 :(得分:0)

你已经告诉DecimalFormat在必须格式化货币时使用哪种货币符号。但你没有告诉它格式化货币。 no-arg构造函数使用的默认模式不是为了格式化货币。使用专用模式。

The javadoc告诉您需要知道的一切。