2012-12-18 94 views
2

我有一個基本問題。我並不擅長編程。我試圖在我的答案字段中顯示2個小數位的答案。我有我的JTextFields和我的JRadioBtn集。當答案來自我的地區或周邊時,有很多小數位。我只需要它來取整2位小數。這裏是我的代碼至今:限制小數位數

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import static java.lang.Math.*; 

public class Triangle extends JFrame 
{ 
private GridBagLayout layout = new GridBagLayout(); 
private GridBagConstraints constraints = new GridBagConstraints(); 
JTextField firstNumberTxt, secondNumberTxt, thirdNumberTxt, answerTxt; 
JRadioButton addBtn, subBtn, multBtn, divBtn; 
JButton calculateBtn, exitBtn; 

public ExamQuestion2() 
{ 
    super("A Simple Calculator Form."); 
    firstNumberTxt = new JTextField(5); 
    secondNumberTxt = new JTextField(5); 
    answerTxt = new JTextField(5); 
    addBtn = new JRadioButton("area", true); 
    subBtn = new JRadioButton("perimeter", false); 

    calculateBtn = new JButton("cal" + 
       "calculate"); 
    exitBtn = new JButton("exit"); 

    JLabel firstNumberLbl = new JLabel("Base: "), 
      operationLbl = new JLabel("Operation "), 
      secondNumberLbl = new JLabel("Height: "), 
      answerLbl =new JLabel("Answer: "); 

    setLayout(layout); 
    addComponent(firstNumberLbl, 0, 0, 1, 1, GridBagConstraints.EAST); 
    addComponent(firstNumberTxt, 0, 1, 1, 1, GridBagConstraints.WEST); 
    addComponent(operationLbl, 1, 0, 1, 4, GridBagConstraints.CENTER); 
    addComponent(addBtn, 1, 1, 1, 1, GridBagConstraints.WEST); 
    addComponent(subBtn, 2, 1, 1, 1, GridBagConstraints.WEST); 
    addComponent(secondNumberLbl, 5, 0, 1, 1, GridBagConstraints.EAST); 
    addComponent(secondNumberTxt, 5, 1, 1, 1, GridBagConstraints.WEST); 
    addComponent(answerLbl, 6, 0, 1, 1, GridBagConstraints.EAST); 
    addComponent(answerTxt, 6, 1, 1, 1, GridBagConstraints.WEST); 
    addComponent(calculateBtn, 7, 0, 1, 1, GridBagConstraints.EAST); 
    addComponent(exitBtn, 7, 1, 1, 1, GridBagConstraints.WEST); 

    calculateBtn.addActionListener(new Calculator()); 
    exitBtn.addActionListener(new Exiter()); 
} 

private void addComponent(Component component, int row, int column, int width, int height, int anch) 
{ 
    constraints.gridx = column; 
    constraints.gridy = row; 
    constraints.gridwidth = width; 
    constraints.gridheight = height; 
    constraints.anchor = anch; 
    constraints.weighty = 5; 
    add(component); 
    layout.setConstraints(component, constraints); 
} 

private class Calculator implements ActionListener 
{ 
    public void actionPerformed(ActionEvent event) 
    { 
     String firstString, secondString; 
     int choice; 
     int firstNumber, secondNumber; 

     firstString = firstNumberTxt.getText(); 
     secondString = secondNumberTxt.getText(); 
     if(firstString.length() < 1 || secondString.length() < 1) 
     { 
      answerTxt.setText(" INVALID"); 
      return; 
     } 
     firstNumber = Integer.parseInt(firstString); 
     secondNumber = Integer.parseInt(secondString); 

     if(addBtn.isSelected()) 
     { 
      choice = 0; 
      subBtn.setSelected(false); 
     } 
     if(addBtn.isSelected()) choice = 0; 
     else if(subBtn.isSelected()) choice = 1; 
     else if(multBtn.isSelected()) choice = 2; 
     else choice = 3; 

     switch (choice) 
     { 
     case 0: answerTxt.setText(((firstNumber * secondNumber)/2) +  ""); break; 
     case 1: answerTxt.setText((firstNumber + secondNumber + sqrt( (firstNumber * firstNumber) + 
       (secondNumber * secondNumber))) + ""); break; 
     default: if (secondNumber != 0) 
       { 
        answerTxt.setText((firstNumber/ secondNumber) + ""); break; 
       } 
     else answerTxt.setText(" INVALID "); break; 

     } 
    } 
} 

public static void main(String[] args) 
{ 
    Triangle MyCalculator= new Triangle(); 
    MyCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    MyCalculator.setSize(300, 300); 
    MyCalculator.setLocationRelativeTo(null); 
    MyCalculator.setVisible(true); 
} 

private class Exiter implements ActionListener 
{ 
    public void actionPerformed(ActionEvent event) 
    { 
     System.exit(0); 
    } 
} 
} 

我已經看過,並試圖我對netbut foind幾件事情似乎無法得到任何工作的權利。任何人幫助?

+0

嘗試'NumberFormat.getCurrencyInstance()';有一個相關的例子[這裏](http://stackoverflow.com/a/13903307/230513)。 – trashgod

+0

你給我的例子給了我很多答案。感謝你的幫助。你是最好的!!!! – user1911494

回答

2

嘗試使用:

String.format("%.2f", numberToBeRounded); 

例如,您可以替換包含case 0這一行:

case 0: answerTxt.setText(String.format("%.2f", (firstNumber * secondNumber)/2)); break; 

%.2f格式說明會顯示四捨五入至小數點後兩位浮點數。有關格式化字符串的更多詳細信息,請參見Formatter文檔。

+0

我認爲轉換'g'和'G'使用精度作爲四捨五入後得到的數值中的_total_位數。您可能需要'e','E'或'f',精度是小數點分隔符後的位數。 – trashgod

+0

謝謝,你是對的。將其從'.2g'改爲'.2f'。 – badjr

+0

請說明爲什麼這個工程。由於它顯示爲低質量的帖子,因此您的回答有被刪除的危險。 –

4

嘗試NumberFormat.getCurrencyInstance();有一個相關的例子here

1

除了NumberFormat用於顯示您的號碼,我建議您退出代碼並查看BigDecimal類以滿足您的數字計算需求。您可以設置比例BigDecimal(BigInteger unscaledVal, int scale)並有效(正確讀取)執行數字計算。