Java 在数字中添加逗号(分组分隔符)而不修改小数?
我正在尝试格式化一个字符串,以便在3位数字组之间添加逗号 例如: 我正试图弄清楚如何使用DecimalFormat,到目前为止,我一直在使用自己的脚本,这似乎过于复杂。我不知道怎么做,使用#只是隐藏尾随的零,使用0将它们添加到数字中 这就是我现在正在尝试的:Java 在数字中添加逗号(分组分隔符)而不修改小数?,java,decimalformat,Java,Decimalformat,我正在尝试格式化一个字符串,以便在3位数字组之间添加逗号 例如: 我正试图弄清楚如何使用DecimalFormat,到目前为止,我一直在使用自己的脚本,这似乎过于复杂。我不知道怎么做,使用#只是隐藏尾随的零,使用0将它们添加到数字中 这就是我现在正在尝试的: DecimalFormat df = new DecimalFormat("###,###.####", new DecimalFormatSymbols(Locale.US)); resultStr = df.format(Double.
DecimalFormat df = new DecimalFormat("###,###.####", new DecimalFormatSymbols(Locale.US));
resultStr = df.format(Double.valueOf(resultStr));
我肯定这一定很容易,但我不知道怎么做。我不必使用DecimalFormat,我只是觉得这会更简单。如何简单地添加逗号而不以任何方式修改小数?您应该使用NumberFormat对象并将其设置为使用分组。差不多
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));
}
}
}
}
DecimalFormat df = new DecimalFormat("#,###.00");
System.out.println(df.format(1200.20));
System.out.println(df.format(15000));
System.out.println(df.format(123456789.99));
返回:
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
这样做的一个优点是,解决方案可以是特定于语言环境的
已编辑现在显示一个DecimalFormat对象的示例。请注意,如果使用此选项,则应设置分组大小。您应该能够完全按照自己的意愿进行操作:
你也可以试试类似的东西
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));
}
}
}
}
DecimalFormat df = new DecimalFormat("#,###.00");
System.out.println(df.format(1200.20));
System.out.println(df.format(15000));
System.out.println(df.format(123456789.99));
1,200.20
15,000.00
123,456,789.99
我无法让任何其他解决方案工作,一些是小数取整,一些是添加尾随零,或是删除尾随零 我不打算接受我自己的答案,但我会发布我的脚本,以防有人发现它有用
public void(String str) {
int floatPos = str.indexOf(".") > -1 ? str.length() - str.indexOf(".") : 0;
int nGroups= (str.length()-floatPos-1-(str.indexOf("-")>-1?1:0))/3;
for(int i=0; i<nGroups; i++){
int commaPos = str.length() - i * 4 - 3 - floatPos;
str = str.substring(0,commaPos) + "," + str.substring(commaPos,str.length());
}
return str;
}
1 => 1
1.0 => 1.0
1234.01 => 1,234.01
1100100.12345 => 1,100,100.12345
public void(字符串str){
int floatPos=str.indexOf(“.”>-1?str.length()-str.indexOf(“.”):0;
int nGroups=(str.length()-floatPos-1-(str.indexOf(“-”>-1-1:0))/3;
对于(int i=0;i 1
1.0 => 1.0
1234.01 => 1,234.01
1100100.12345 => 1,100,100.12345
最简单的解决方案
为什么不使用带有printf
System.out.printf( "%,d\n", 58625 );// the d to accept decimal integer
System.out.printf( "%,.2f", 12345678.9 );// the f to accept folouting point and 2 to take only 2 digits
输出将是
58,625
12,345,678.90
好消息是:实际生成的分隔符
特定于用户的区域设置
ADecimalFormat
是一个NumberFormat
@A\u horse\u,名称为:true,他可以调用setGroupingUsed(true)
在DecimalFormat对象上。虽然使用了DecimalFormat,我还是使用了该代码。但是它在组中使用了“.”而不是逗号。如何将其更改为“.”或Locale.US?@Liso22:是的,解决方案是特定于区域设置的。请确保为使用逗号作为分组分隔符的区域设置了NumberFormat或DecimalFormat对象的区域设置。I just尝试过。在我尝试使用4位小数的数字并开始舍入值之前,一切都正常。例如:1.1237变为:1.124。你知道解决这个问题的方法吗?最好调用setGroupingUsed(true)
和setGroupingSize(3)
否则,您的解决方案对大于999999.00的数字无效。@HovercraftFullOfEels似乎对此类数字有效,例如999999.00
->999999.00
。我收回我说过的话。1+这是我今天最后一次投票了(
58,625
12,345,678.90