这两个字符表示什么意思

fangz1080 2011-09-20 10:19:44
public class Interest
{
public static void main(String args[])
{
double amount;
double principle = 1000.0;
double rate = 0.05;
System.out.printf("%s%20s\n","Year","Amount on deposit");
for(int year = 1; year <= 10;year++)
{
amount = principle * Math.pow( 1.0 + rate ,year);
System.out.printf("%4d%,20.2f\n",year,amount);
}

}
}
"%s%20s\n"这个字符中的20表示什么意思?
"%4d%,20.2f\n"这个字符转换是什么意思?20.2f是什么意思?
...全文
128 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
coooliang 2011-09-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fainfy 的回复:]
java.util.Formatter
查API就明白了。因为Javadoc太多了不能贴上来。
[/Quote]
有帮助!
fainfy 2011-09-20
  • 打赏
  • 举报
回复
java.util.Formatter
查API就明白了。因为Javadoc太多了不能贴上来。
huntor 2011-09-20
  • 打赏
  • 举报
回复
既然使用了 printf ,把 \n 改为 %n 吧。
  • 打赏
  • 举报
回复
最后的 \n 最好能改成 %n
scrack 2011-09-20
  • 打赏
  • 举报
回复
学习了
打油的程序员 2011-09-20
  • 打赏
  • 举报
回复

//格式 [argument_index$][flags][width][.precision]conversion
//还可以写成
System.out.format("%4d%,20.2f\n",year,amount);
//或
System.out.print(String.format("%4d%,20.2f\n",year,amount));
//或
System.out.println(String.format("%4d%,20.2f\n",year,amount));



打油的程序员 2011-09-20
  • 打赏
  • 举报
回复
1楼正解
20 宽度
.2 精度
还可以写成

System.out.format("%4d%,20.2f\n",year,amount);
//或
System.out.format(String.format("%4d%,20.2f\n",year,amount));

magicluo 2011-09-20
  • 打赏
  • 举报
回复
java5中的推出的类似C语言printf风格的格式化输出

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧