简体   繁体   English

Java —将钱四舍五入到小数点后两位

[英]Java — Round money to 2 decimal places

I want to round the variable retail_price to 2 decimal places. 我想将变量retail_price舍入到2个小数位。 I have tried a few times and it always gives me an error. 我尝试了几次,它总是给我一个错误。 As far as I know there are two ways to round to 2 decimal places. 据我所知,有两种方法可以舍入到小数点后两位。 For this instance I really only need to use it once. 对于这种情况,我真的只需要使用一次。 Would it still be better to import it or no?? 导入还是不导入更好? Help 救命

        // The actionPerformed method executes when the user clicks the calculate button
    public void actionPerformed(ActionEvent e)
    {
        double retail_price;
        String input1;
        String input2;

        // Get the number from the users input in priceFeild1
        input1 = priceField1.getText();

        // Get the number from the users input in priceFeild2
        input2 = priceField2.getText();

        // Calculate out the priceFeilds to find the retail price.
        retail_price = Double.parseDouble(input1) + ((Double.parseDouble(input1) * (Double.parseDouble(input2) / 100)));

        // Show the results
        JOptionPane.showMessageDialog(null, "The whole sale cost of the item is: $" + input1 + ". "
                                                + "\nThe markup percentage of the item is: " +  input2 + "%." 
                                                + "\nIf those two numbers were correct, the retail price is: $" + retail_price);            
    }

You can use DecimalFormat to achieve that. 您可以使用DecimalFormat来实现。 First initialize it like that: 首先像这样初始化它:

final DecimalFormat df = new DecimalFormat("#.##");

than instead of printing retail_price use this: 而不是打印Retail_price而不是使用此:

df.format(retail_price)

Remember that if you want to have a reliable money calculations you should use BigDecimal instead of double. 请记住,如果您想获得可靠的货币计算,则应使用BigDecimal而不是double。

Try 尝试

// Show the results
        JOptionPane.showMessageDialog(null, String.format("The whole sale cost of the item is: $%.2f. "
                                                + "\nThe markup percentage of the item is: %s%%." 
                                                + "\nIf those two numbers were correct, the retail price is: $%.2f", Double.parseDouble(input1), input2, retail_price));  

这将进行舍入。

retail_price = Math.round(retail_price * 100.0) / 100.0;

Here's how I did it in one line. 这是我一行完成的操作。

double amount = Double.Parse(String.format("%.2f", 20.4425);

For basic purposes this is more than enough. 从根本上来说,这已经足够了。 Let me break it down. 让我分解一下。

double amountIn = 20.4425;
String amountInString = String.format("%.2f", amountIn);
double amountOut = Double.parse(amountInString);

String.format() converts the double into a 2 decimal string, then Parse turns it back into a double. String.format()将双精度型转换为两位十进制字符串,然后Parse将其转换为双精度型。 Simple efficient and easy to do. 简单高效且易于执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM