2015-06-08 64 views
-2

我有一个简单计算代码,计算关于盒体积(长×宽×高):的java设置双精度值格式

ActionListener volListener = new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     try{ 
      String L = long.getText(); 
      String W = width.getText(); 
      String H = height.getText(); 
      double parseL = Double.parseDouble(L); 
      double parseW = Double.parseDouble(W); 
      double parseH = Double.parseDouble(H); 
      double volResult = parseL*parseW*parseH; 
      txtAreaOut.append("Volume : " + volResult + "\n"); 
      long.setText(""); 
      width.setText(""); 
      height.setText(""); 
     }catch(NumberFormatException nfe){ 
      String fault = nfe.getMessage(); 
      infoBox(fault, " " + nfe.getClass()); 
     } 
    } 
}; 

如果“volResult”返回大量,例如9.0408284742E7, 和我希望这样9.040.828.474结果...

代码如何?..

+1

在这里检查,有答案显示整数和/或格式数字字符串,如果你想逗号。 http://stackoverflow.com/questions/9602444/force-a-double-to-write-the-whole-number – theguywhodreams

+0

这是重复的很多问题是重复的另一个问题,我甚至不知道要选择哪个... – zubergu

回答

0

使用的String.Format和定义小数位数。这里我定义了两个小数位。

txtAreaOut.append(“Volume:”+ String.format(“%.2f”,volResult)+“\ n”);