Java 浮点数格式问题

Java 浮点数格式问题,java,numbers,number-formatting,decimalformat,Java,Numbers,Number Formatting,Decimalformat,我正在使用以下代码: DecimalFormat df = new DecimalFormat(); df.setMinimumFractionDigits(2); df.setMaximumFractionDigits(2); float a=(float) 15000.345; Sytem.out.println(df.format(a)); 我得到这个输出:15000.35 我不希望逗号出现在这个输出中。 我的输出应该是:15000.35 用Java获取此输出的最佳方法是什么?阅读jav

我正在使用以下代码:

DecimalFormat df = new DecimalFormat();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
float a=(float) 15000.345;
Sytem.out.println(df.format(a));
我得到这个输出:15000.35 我不希望逗号出现在这个输出中。 我的输出应该是:15000.35


用Java获取此输出的最佳方法是什么?

阅读javadoc并使用以下内容:


df.setGroupingUserFalse

应设置分组大小。默认值为3。见

或者使用setGroupingUsed

  df.setGroupingUsed(false);
您的完整代码

DecimalFormat df = new DecimalFormat();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
df.setGroupingUsed(false);
float a=(float) 15000.345;
Sytem.out.println(df.format(a));
试一试

你也可以通过。作为模式

DecimalFormat df = new DecimalFormat("#####.##");

您可以这样做:

DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
之后,正如您所做的:

df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
float a=(float) 15000.345;
System.out.println(df.format(a));
这将为您提供预期的结果

DecimalFormat df = new DecimalFormat("#####.##");
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
float a=(float) 15000.345;
System.out.println(df.format(a));