格式化控制台输出

来源:互联网 发布:js 跨域 面试 编辑:程序博客网 时间:2024/05/15 13:45
PrintStream java.io.PrintStream.printf(String format,Object... args)

      A convenience method to write a formatted string to this output stream using the specified format string and arguments.

     An invocation of this method of the form out.printf(format, args) behaves in exactly the same way as the invocation 

   

        System.out.printf(format,item1,item2,item3,..........,itemk)

                               常用的标识符

     标识符        输出                                                 举例

    %b             布尔值                                             true 或false

   %c               字符                                                  'a'

   %d              十进制整数                                      200

   %f               浮点数                                             45.460000

  %e             标准科学记数法形式的数              4.556000e+01

  %s               字符串                                            “Java is cool”

                         指定宽度和精度的例子

%5c                 输出字符并在这个字符条目前面加4个空格

%6b                 输出布尔值,在false前加一个空格,在true前加俩个空格

%5d               输出整数条目,宽度至少为5.如果该条目的数字位数小于5,就在前面加空格,如果该条目的位数大于5,则自动增加宽度

%10.2f            输出的浮点数宽度至少为10,包括小数点和小数点后俩位。这样,给小数点前分配了7位。如果该项小数点前的位数小于7,就在数字前面加空格。如果该项小数点前的位数大于7,则自动增加宽度

%12s           输出的字符串宽度至少为12.如果该字符串条目小于12个字符,就在该条目前加空格。如果该字符串条目多于12个字符,则自动增加宽度。

0 0
原创粉丝点击