问题描述
我正在尝试设置字符串格式以在3位数字组之间添加逗号
I'm trying to format a string to add commas between 3 digit groups
EG:
1200.20 >> 1,200.20
15000 >> 15,000
我试图弄清楚如何使用DecimalFormat,到目前为止,我一直在使用自己的脚本,该脚本似乎过于复杂.我不知道该怎么做,使用#只是隐藏结尾的零,而使用0会将它们添加到数字中.
I'm trying to figure out how to do it with DecimalFormat, to this point I have been using a script of my own that seems overly complicated. I cannot figure out how to do it, using # simply hides trailing zeroes and using 0 adds them to the number.
这就是我现在正在尝试的内容:
This is what I'm trying right now:
DecimalFormat df = new DecimalFormat("###,###.####", new DecimalFormatSymbols(Locale.US));
resultStr = df.format(Double.valueOf(resultStr));
我确定它一定很容易,但是我不确定该怎么做.我不必使用DecimalFormat进行操作,我只是认为这将是更简单的方法.如何简单地添加逗号而不用任何方式修改小数?
I'm sure it must be easy but I'm not sure how to do it. I don't have to do it with DecimalFormat, I just thought it would be the easier way. How can I simply add the commas without modifying the decimals in any way?
推荐答案
您应该使用NumberFormat对象并将其设置为使用分组.像
You should use a NumberFormat object and set it to use grouping. Something like
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class NumberFormatEg {
public static void main(String[] args) {
NumberFormat myFormat = NumberFormat.getInstance();
myFormat.setGroupingUsed(true);
double[] numbers = { 11220.00, 232323232.24, 121211.55, 102.121212 };
for (double d : numbers) {
System.out.println(myFormat.format(d));
}
System.out.println();
DecimalFormat decimalFormat = new DecimalFormat("#.00");
decimalFormat.setGroupingUsed(true);
decimalFormat.setGroupingSize(3);
for (double d : numbers) {
System.out.println(decimalFormat.format(d));
}
System.out.println("\nFor Germany");
NumberFormat anotherFormat = NumberFormat
.getNumberInstance(Locale.GERMAN);
if (anotherFormat instanceof DecimalFormat) {
DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
anotherDFormat.applyPattern("#.00");
anotherDFormat.setGroupingUsed(true);
anotherDFormat.setGroupingSize(3);
for (double d : numbers) {
System.out.println(anotherDFormat.format(d));
}
}
System.out.println("\nFor US:");
anotherFormat = NumberFormat.getNumberInstance(Locale.US);
if (anotherFormat instanceof DecimalFormat) {
DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
anotherDFormat.applyPattern("#.00");
anotherDFormat.setGroupingUsed(true);
anotherDFormat.setGroupingSize(3);
for (double d : numbers) {
System.out.println(anotherDFormat.format(d));
}
}
}
}
返回:
11,220
232,323,232.24
121,211.55
102.121
11,220.00
232,323,232.24
121,211.55
102.12
For Germany
11.220,00
232.323.232,24
121.211,55
102,12
For US:
11,220.00
232,323,232.24
121,211.55
102.12
这样做的一个优点是解决方案可以是特定于语言环境的.
An advantage of this is that the solution can be locale specific.
已编辑
现在显示一个带有DecimalFormat对象的示例.请注意,如果使用此选项,则应设置分组大小.
Edited
Now shows an example with a DecimalFormat object. Note that you should set the grouping size if you use this.
这篇关于将逗号(分组分隔符)添加到数字而不修改小数位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!