如何在JOptionPane中格式化字符串

时间:2021-06-02 20:47:01

I found and fixed my error, i was formatting twice.

我找到并修复了我的错误,我正在格式化两次。

I am trying to display a dollar amount using JOptionPane, but since it is a double, I am getting extra digits after the decimal. How do I format while within JOptionPane? I tried using String.format but I am getting an error when I run it;

我试图使用JOptionPane显示一个美元金额,但因为它是一个双倍,我得到十进制后的额外数字。如何在JOptionPane中格式化?我尝试使用String.format,但是当我运行它时出现错误;

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%.2f'

线程“main”中的异常java.util.MissingFormatArgumentException:格式说明符'%。2f'

 at java.util.Formatter.format(Unknown Source)
 at java.util.Formatter.format(Unknown Source)
 at java.lang.String.format(Unknown Source)
 at CouponCalc.main(CouponCalc.java:38)

Here is my code;

这是我的代码;

import javax.swing.JOptionPane;
public class CouponCalc {
    public static void main(String[] args) {

        double groceriesCost, discount;
        double tier1=.08, tier2=.1, tier3=.12, tier4=.14; //coupon percentages for easy adjustment

        groceriesCost = Double.parseDouble(JOptionPane.showInputDialog("Enter cost of groceries."));

        if (groceriesCost<0)
            {
                JOptionPane.showMessageDialog(null, "Invalid Entry"); //TODO implement loop
                System.exit(0);
            }

        else if (groceriesCost>=0 && groceriesCost<=9.99)   //No coupon
            JOptionPane.showMessageDialog(null, "You do not qualify for any coupons.)");


        else if (groceriesCost>=10.00 && groceriesCost<=59.99) //tier 1 coupon  
            JOptionPane.showMessageDialog(null, String.format("You win a discount coupon of $" + String.format("%.2f", (groceriesCost*tier1)) +
                                                ". (8% of your purchase.)"));
        else if (groceriesCost>=60.00 && groceriesCost<=149.99) //tier 2 coupon
            JOptionPane.showMessageDialog(null, String.format("You win a discount coupon of $" + String.format("%.2f", (groceriesCost*tier2)) +
                                                ". (10% of your purchase.)"));
        else if (groceriesCost>=150.00 && groceriesCost<=209.99) //tier 3 coupon
            JOptionPane.showMessageDialog(null, String.format("You win a discount coupon of $" + String.format("%.2f", (groceriesCost*tier3)) +
                                                ". (12% of your purchase.)"));
        else if (groceriesCost>=210.00) //tier 4 coupon
            JOptionPane.showMessageDialog(null, String.format("You win a discount of $" + String.format("%.2f", (groceriesCost*tier4)) +
                                                ". (14% of your purchase.)"));

        System.exit(0);
    }
}

1 个解决方案

#1


0  

You should not format the whole string. Only the double.

你不应该格式化整个字符串。只有双倍。

 JOptionPane.showMessageDialog(null, "You win a discount coupon of $" + String.format("%.2f", (groceriesCost*tier1)) +
                                            ". (8% of your purchase.)");

#1


0  

You should not format the whole string. Only the double.

你不应该格式化整个字符串。只有双倍。

 JOptionPane.showMessageDialog(null, "You win a discount coupon of $" + String.format("%.2f", (groceriesCost*tier1)) +
                                            ". (8% of your purchase.)");