Search code examples
javajframerounding

rounding a number to the hundredth place


I want to round the output to the hundredth place but have failed to do so.

Here is the code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Assignment2Part2 extends JFrame{ 


//window
private static final int WIDTH = 550;
private static final int HEIGHT = 400;
    
private JLabel firstNameL, lastNameL, milesL, costL, mpgL, dailycostL;//labels for all the variables
private JTextField firstNameTF, lastNameTF, milesTF, costTF, mpgTF, dailycostTF;//text fields for all the variables
private JButton calculateB, exitB;

private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;

    
public Assignment2Part2 ()
{
 
    setTitle("Find your daily cost of driving");
    
//labels
    firstNameL = new JLabel("First Name ", SwingConstants.CENTER);
    lastNameL = new JLabel("Last Name ",SwingConstants.CENTER);
    milesL = new JLabel("Total miles driven per day ",SwingConstants.CENTER);
    costL = new JLabel("Cost per gallon of gas ",SwingConstants.CENTER);
    mpgL = new JLabel("Average MPG ",SwingConstants.CENTER);
    dailycostL = new JLabel("Daily cost of driving is: ",SwingConstants.CENTER);
    
//text fields
    firstNameTF = new JTextField();
    lastNameTF = new JTextField();
    milesTF = new JTextField();
    costTF = new JTextField();
    mpgTF = new JTextField();
    dailycostTF = new JTextField();
    
//find button
    calculateB = new JButton("Find");
    cbHandler = new CalculateButtonHandler();
    calculateB.addActionListener(cbHandler);
    
//exit button
    exitB = new JButton("Exit");
    ebHandler = new ExitButtonHandler();
    exitB.addActionListener(ebHandler);
    
    Container pane = getContentPane();
    
    pane.setLayout(new GridLayout(8, 4));
    
//panes
    pane.add(firstNameL);
    pane.add(firstNameTF);
    pane.add(lastNameL);
    pane.add(lastNameTF);
    pane.add(milesL);
    pane.add(milesTF);
    pane.add(costL);
    pane.add(costTF);
    pane.add(mpgL);
    pane.add(mpgTF);
    pane.add(dailycostL);
    pane.add(dailycostTF);
    pane.add(calculateB);
    pane.add(exitB);
    
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setLocationRelativeTo(null);
}

//find button
    private class CalculateButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
        //variables 
            double first, last, total, cost, averagempg, dailycost; 
        
        //strings to doubles
            total = Double.parseDouble(milesTF.getText());
            cost = Double.parseDouble(costTF.getText());
            averagempg = Double.parseDouble(mpgTF.getText());
        //calculates cost
            dailycost = (total * cost)/averagempg;
        //outputs text
            dailycostTF.setText("$" + dailycost);
        }
    }
//exit button
    private class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }
    }           
    public static void main(String[] args){
    Assignment2Part2 rectObject = new Assignment2Part2();
    }
}

the output line being

dailycostTF.setText("$" + dailycost);

Any help would be great! I am completely new to Java.


Solution

  • An easy way is to use either the number format or decimal format class.

        NumberFormat dollars = new NumberFormat.getCurrencyInstance();
    

    Then you can format numbers into dollars quite easily. No clunky "$" needed.