我是一个新手java的家伙试图让我的打印输出语句格式化十进制位置..我知道%。2f有效,但无论出于何种原因,当我尝试应用%.2f时,它就是炸弹....不知道该怎么做..有什么建议吗?提前谢谢!
credits = int
raise = double
System.out.printf("An undergraduate resident student taking " + credits + " currently pays $" + "%,.2f",credtotal);
System.out.println("\n");
System.out.printf("with a increase in tuition of " + (trying to format here) raise + ", per credit will become " + raisecredund + ", and an undergraduate resident taking " + credits + " credits" + " will pay $" +"%,.2f",(credits*(raise*245.73))+ credtotal);;
答案 0 :(得分:0)
final String str1 = String.format("An undergraduate resident student taking %d currently pays $%.2f",credits, credtotal);
编辑:我没有注意到您使用的是printf
。你很亲密:
System.out.printf("An undergraduate resident student taking %d currently pays $%.2f", credits,credtotal);
使用第一个示例字符串,您可以内联提供格式化字符,然后为每个占位符提供参数。假设credtotal
为双倍且知道credits
为int
,则上述将产生(分配相应的值)
An undergraduate resident student taking 1 currently pays $2.00
答案 1 :(得分:0)
您似乎正在使用"%,.2f"
逗号,我认为这不是合法的。 %.2f
会将数字打印到2位小数。
如果你想在钱中加入逗号,你应该使用DecimalFormat
DecimalFormat formatter = new DecimalFormat("#,###.00");