1

我想将输出四舍五入到百分之一,但没有这样做。

这是代码:

    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();
    }
}

输出线是

dailycostTF.setText("$" + dailycost);

所以有一些其他的方法,但没有运气。任何帮助都会很棒!我对 java 完全陌生,而且只在我上 java 课的第二周。-先谢谢了 !

4

2 回答 2

3

一种简单的方法是使用数字格式或十进制格式类。

    NumberFormat dollars = new NumberFormat.getCurrencyInstance();

然后你可以很容易地将数字格式化为美元。不需要笨重的“$”。

于 2013-09-11T21:09:05.970 回答
1
DecimalFormat df = new DecimalFormat("#.##");      
dailyCost = Double.valueOf(df.format(dailyCost));
于 2013-09-11T20:55:36.020 回答