[英]How to use add dot in Java's DecimalFormat to currency?
I'm trying to find a way to display currency with a dot so for instance it should be我正在尝试找到一种用点显示货币的方法,例如它应该是
1.234,56 kr. 1.234,56 克朗。
At the moment I'm using目前我正在使用
pattern = "#,##0.00 ¤";
new DecimalFormat(pattern);
This doesn't work as the Danish krone is for some reason defined there as kr instead of officially recognized kr.这不起作用,因为丹麦克朗出于某种原因在那里被定义为 kr 而不是官方认可的 kr。
I've looked for a way to escape these characters using Unicode value that I would add to pattern but that doesn't work.我一直在寻找一种方法来使用我将添加到模式中的 Unicode 值来转义这些字符,但这不起作用。 In the official documentation here I don't see a way to do it either.
在此处的官方文档中,我也看不到这样做的方法。
TLDR: I want to add full stop after currency symbol. TLDR:我想在货币符号后添加句号。 So at the moment I have it like this
kr
, what I want to get is kr.
所以目前我有这样的
kr
,我想得到的是kr.
. .
If you just want the dot after the currency symbol you can set a custom currency symbol.如果您只想要货币符号后的点,您可以设置自定义货币符号。 Here I did it using euros (note that instead of "€." you can put my_symbol.getCurrencySymbol()+"." if you want it for all currency and not just one):
在这里,我使用欧元(请注意,不是“€.”,你可以输入 my_symbol.getCurrencySymbol()+“.”,如果你想要它用于所有货币而不仅仅是一种):
DecimalFormatSymbols my_symbol = new DecimalFormatSymbols();
my_symbol.setCurrencySymbol("€.");
String pattern = "###,###.###¤";
DecimalFormat decimalFormat1 = new DecimalFormat(pattern, my_symbol);
String format = decimalFormat1.format(987654321.321);
System.out.println(format);
This gives这给
987.654.321,321€.
The output of currency values depends on the respective country setting. output 的货币值取决于各自的国家设置。 If you want to explicitly have a decimal point as in your case, you have to set a corresponding locale for the
Numberformat
.如果你想像你的情况一样明确地有一个小数点,你必须为
Numberformat
设置一个相应的语言环境。 Eg English for a point.例如英语要点。
For Example:例如:
public String FormatWithPoint(double yourValue){
NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
nf.setGroupingUsed(false);
return nf.format(yourValue);
}
Edit: If you need more control, you can also do the following:编辑:如果您需要更多控制,您还可以执行以下操作:
public String FormatCurreny(double yourValue){
NumberFormat nf = NumberFormat.getCurrencyInstance();
nf.setGroupingUsed(false);
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("kr.");
dfs.setMonetaryDecimalSeparator('.');
((DecimalFormat) nf).setDecimalFormatSymbols(dfs);
return nf.format(yourValue);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.