【发布时间】:2015-02-25 02:49:24
【问题描述】:
我正在使用 printf 方法进行类似收据的控制台输出。 它列出了项目、价格和数量,然后是总数。
我目前已经按照我想要的方式排列了所有内容,尽管它没有显示 2 个小数位。我需要显示完整的数字,例如:“$1.00”,目前显示的是“$1”。
这就是我所拥有的
System.out.printf("%-30s %-10s %-10s %-5s", "Item","Quantity","Price","Total\n");
System.out.printf("%-30s %-10.2f %-10.2s %-5.2s %s", "Gum",gumQuantity,"$"+gumPrice,"$"+gumTotal,"\n");
System.out.printf("%-30s %-10.2f %-10.2s %-5.2s %s", "Soda",sodaQuantity,"$"+sodaPrice,"$"+sodaTotal,"\n");
System.out.printf("%-30s %-10.2f %-10.2s %-5.2s %s", "Chips",chipsQuantity,"$"+chipsPrice,"$"+chipsTotal,"\n");
System.out.printf("%-52s %-5.2s %s", "Subtotal", "$"+subTotal,"\n");
System.out.printf("%-52s %-5.2s %s", "Sales Tax", "$"+salesTax,"\n");
System.out.printf("%-52s %-5.2s %s", "Total", "$"+totalFinal,"\n");
输出看起来像这样
Item Quantity Price Total
Gum 10.00 $1 $1
Soda 3.00 $1 $3
Chips 20.00 $1 $2
Subtotal $4
Sales Tax $3
Total $4
我怎样才能让它显示完整的值?
【问题讨论】: