未知格式转换异常?

2 java printf

我有以下代码.在运行时,只要此方法执行生成,就会生成异常UnknownFormatConversionException.我试图将值显示为2位小数.

public void totalPrice(Vector <Stock> temp){
    if(!out){
        cst = temp.elementAt(l).getCost();
        cost = Double.parseDouble(cst);
        tax = ((cost * q) * 2)/100;
        price = (cost * q) + tax;
        System.out.printf ("Product Cost: € %.2f\n", (cost * q));
        System.out.printf ("Tax Total (2% rate): € %.2f\n", tax);
        System.out.printf ("Net Cost: € %.2f\n", price); 
        System.out.println ("============================================="); 
        System.out.println (); 
    }
}
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 10

使用%%时要使用%的文字.此外,虽然不会引起你的错误,你将要使用%n没有\n.

System.out.printf ("Tax Total (2%% rate): € %.2f%n", tax);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅Formatter API.

此外,如果您想显示货币,最好使用a NumberFormat.getCurrencyInstance(Locale someLocale).