2015-06-08 98 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」);