java 将数字四舍五入到第一百位

wbgh16ku  于 2023-01-19  发布在  Java
关注(0)|答案(2)|浏览(141)

我想把产量四舍五入到第100位,但没有成功。
下面是代码:

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完全是新手。

0qx6xfy6

0qx6xfy61#

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

NumberFormat dollars = new NumberFormat.getCurrencyInstance();

这样你就可以很容易地将数字格式化成美元,而不需要笨重的“$”。

yfwxisqw

yfwxisqw2#

DecimalFormat df = new DecimalFormat("#.##");      
dailyCost = Double.valueOf(df.format(dailyCost));

相关问题