title: Limiting decimal places
body: <p>I have a basic question. I'm not really good at programming. I'm trying to display an answer to 2 decimal places in my answer field. I have my JTextFields and my JRadioBtn set. When the answer comes for for my area or perimeter, there are lots of decimal places. I just need it to round off the 2 decimal places. Here is the code I have so far:</p>
  
  <pre><code>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() &lt; 1 || secondString.length() &lt; 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);
      }
  }
  }
  </code></pre>
  
  <p>I have looked and tried a few things I have foind on the netbut cannot seem to get anything to work right. Anyone help?</p>