当前位置: 代码迷 >> 综合 >> System.out.format()用法示例
  详细解决方案

System.out.format()用法示例

热度:31   发布时间:2024-01-31 11:36:42.0
public class Main {public static void main(String[] args) {int year = 2020;       //直接打印数字System.out.println(year);System.out.println("----------------");//直接打印数字System.out.format("%d%n",year);//总长度是8,默认右对齐System.out.format("%8d%n",year);//总长度是8,左对齐System.out.format("%-8d%n",year);//总长度是8,不够补0System.out.format("%08d%n",year);//千位分隔符System.out.format("%,8d%n",year*10000);//保留5位小数System.out.format("%.5f%n",Math.PI);//不换行System.out.format("%.5f",Math.PI);System.out.println("----没有换行哟-----");}
}

输出:

2020
----------------
20202020
2020    
00002020
20,200,000
3.14159
3.14159----没有换行哟-----

 

  相关解决方案