我正在尝试在程序中使用 java 的NumberFormat类和getPercentInstance方法来计算税款。我希望程序显示的是带两位小数的百分比。现在,当我之前尝试将小数格式化为百分比时,Java 将 0.0625 显示为 6%。如何让 Java 显示这样的小数或将 0.0625 显示为“6.25%”?
代码片段:
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
NumberFormat fmt2 = NumberFormat.getPercentInstance();
System.out.print("Enter the quantity of items to be purchased: ");
quantity = scan.nextInt();
System.out.print("Enter the unit price: ");
unitPrice = scan.nextDouble();
subtotal = quantity * unitPrice;
final double TAX_RATE = .0625;
tax = subtotal * TAX_RATE;
totalCost = subtotal + tax;
System.out.println("Subtotal: " + fmt1.format(subtotal));
System.out.println("Tax: " + fmt1.format(tax) + " at " + fmt2.format(TAX_RATE));
System.out.println("Total: " + fmt1.format(totalCost));