2012-12-18 69 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)并有效(正确读取)执行数字计算。