这是我的代码。我希望在每个值旁边打印美元符号。
for(int i = 0; i < enrollment.size(); i++)
{
System.out.println(enrollment.get(i).toString());
System.out.printf("\tTuition: %17.2f \n",enrollment.get(i).calcTuition());
}
答案 0 :(得分:0)
只需将$
嵌入格式字符串中:
System.out.printf("\tTuition: $%17.2f \n",enrollment.get(i).calcTuition());
答案 1 :(得分:0)
一种方法是使用NumberFormat货币实例
NumberFormat currencyIntance = NumberFormat.getCurrencyInstance();
double tuition = enrollment.get(i). calcTuition();
System.out.printf("%8s: %9s%n", "Tuition", currencyInstance.format(tuition));
注意,请考虑避免在printf中使用\t
和\n
。使用\t
和%n
的格式说明符宽度代替\n
。
答案 2 :(得分:0)
试试这个:
for (int i = 0; i < enrollment.size(); i++) {
System.out.println(enrollment.get(i).toString());
System.out.printf("\tTuition: %s \n", String.format("%17.2f",
enrollment.get(i).calcTuition()).trim());
}