2013-05-10 129 views
0

我使用这个代码:浮点数格式问题

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

我得到这样的输出:15,000.35 我不想被进来这个输出逗号。 我的输出应该是:15000.35

在Java中获取此输出的最佳方式是什么?

回答

3

尝试

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

Sytem.out.println(df.format(a)); //wrong //sytem 

System.out.println(df.format(a));//correct //System 
5

阅读javadoc和使用:

df.setGroupingUsed(false);

1

分组大小应设置。默认值为3.请参阅Doc.

df.setGroupingSize(0); 

或者您使用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)); 
0

您也可以通过#####.##作为图案

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

你可以这样说:

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)); 

这会给你想要的结果。