遵循以下格式..
System.out.printf("%.2f", val);
想知道我怎样才能让我的输出为左侧的2位小数而不是右侧?我想计算每月利率。我班级方法中的公式是:
return (annualInterestRate / 12) * balance;
然后当我回到main并尝试按如下方式打印时:
"\nAnnual Rate:\t" + String.format("%.2f", a.getAnnualRate())
结果
ID: 1122
Created: 03/02/14
Owner: Tom H
Opening Balance: $100.0
Annual Rate: 4.50
Monthly Interest: 37.5 <-----Would like this to display the monthly interest properly
答案 0 :(得分:0)
Yes, you can format your output使用System.out.printf()
或使用System.out.format()
。
Monthly Interest: 37.5 <-----Would like this to display the monthly interest properly
现在,您在评论中提到0.375
,然后在此处显示37.5
。很明显,代表价值的单位会发生变化,请记住这一点。