Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 322|回复: 0

[Java基础知识]JDK5新特性之二----新的格式化输出

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-1 07:54:46 | 显示全部楼层 |阅读模式
    JDK5.0允许象C语言那样直接用printf()方法来格式化输出,并且提供了许多参数来格式化输入,调用也很简单:

      
       
           System.out.format("Pi is approximately  %f", Math.Pi);    System.out.printf("Pi is approximately  %f", Math.Pi);  
    1. printf()和
    复制代码
    1. format()
    复制代码
    方法具有相同的功能.
    1. System.out
    复制代码
    1. java.io.PrintStream的实例
    复制代码
    .
    1. PrintStream
    复制代码
    ,
    1. java.io.PrintWriter
    复制代码
    , 和
    1. java.lang.String
    复制代码
    每个类都有四个新的格式化方法:
            format( String format, Object... args); printf( String format, Object... args); format( Locale locale, String format, Object... args); printf( Locale locale, String format, Object... args);  
          
       
      

    同时,以前的formatter类也提供了更完善的方法来格式化,例如:
      

      
       
        formatter.format("Pi is approximately %1$f," + "and e is about %2$f", Math.PI, Math.E);   
          
       
      
    格式化元素的构成如下:

      
       
        %[argument_index$][flags][width][.precision]conversion
           其中: argument_index是一个正整数,说明了参数的位置,1为取第一个参数
           width表示输出的最小字母个数
           precision代表数字的小数位数
           conversion代表被格式化的参数的类型:
          
    1. f
    复制代码
    float,
          
    1. t
    复制代码
    time
          
    1. d
    复制代码
    decimal
          
    1. o
    复制代码
    octal
          
    1. x
    复制代码
    hexadecimal
          
    1. s
    复制代码
    general
          
    1. c
    复制代码
    a Unicode character
          
       
      

    以下是个例子:

      
       
        package format;
            
              import java.util.Formatter;
            
              public class UsingFormatter {
            
                public static void main(String[] args) {
                  if (args.length != 1) {
                    System.err.println("usage: " +
                      "java format/UsingFormatter ");
                    System.exit(0);
                  }
                  String format = args[0];
            
                  StringBuilder stringBuilder = new StringBuilder();
                  Formatter formatter = new Formatter(stringBuilder);
                  formatter.format("Pi is approximately " + format +
                    ", and e is about " + format, Math.PI, Math.E);
                  System.out.println(stringBuilder);
                }
              }
            
           //控制台调用
           java format/UsingFormatter %f
           //输出
           Pi is approximately 3.141593, and e is about 2.718282
           //控制台调用
           java format/UsingFormatter %.2f
           //输出
           Pi is approximately 3.14, and e is about 2.72
           //控制台调用
           java format/UsingFormatter %6.2f
           //输出(有空格来填补长度)
           Pi is approximately   3.14, and e is about   2.72
           //控制台调用
           java format/UsingFormatter
    1.   %1$.2f
    2.       
    复制代码
    1. //
    复制代码
    1. 输出
    复制代码
    1.       
    复制代码
    Pi is approximately 3.14, and e is about 3.14
            
           //改变区域设置
           package format;
            
              import java.util.Formatter;
              import java.util.Locale;
            
              public class UsingFormatter {
            
                public static void main(String[] args) {
                  if (args.length != 1) {
                    System.err.println("usage: " +
                      "java format/UsingFormatter <format string>");
                    System.exit(0);
                  }
                  String format = args[0];
            
                  StringBuilder stringBuilder = new StringBuilder();
                  Formatter formatter = new Formatter(stringBuilder,
                                              Locale.FRANCE);
                  formatter.format("Pi is approximately " + format +
                    ", and e is about " + format, Math.PI, Math.E);
                  System.out.println(stringBuilder);
                }
              }
           //控制台调用
           java format/UsingFormatter %.2f
           //输出
           Pi is approximately 3,14, and e is about 2,72
            
           //采用format,printf的可替代写法
           package format;
            
              public class UsingSystemOut {
            
                public static void main(String[] args) {
                  if (args.length != 1) {
                    System.err.println("usage: " +
                      "java format/UsingSystemOut <format string>");
                    System.exit(0);
                  }
                  String format = args[0];
            
                  System.out.format("Pi is approximately " + format +
                    ", and e is approximately " + format, Math.PI, Math.E);
                }
              }
           //控制台调用
           java format/UsingSystemOut %.2f%n
           //输出
           Pi is approximately 3.14
              , and e is about 2.72
            
          
       
      

    对时间的格式化用字母t来代表,通常在t后面加上特殊的字符来显示时间的某个部分:

      
       
        tr  hour and minute,
          
    1. tA
    复制代码
    the day of the week
          
    1. tB
    复制代码
    the name of the month
          
    1. te
    复制代码
    the number of the day of the month
          
    1. tY
    复制代码
    the year
           //eg.
           package format;
            
              import java.util.Calendar;
            
              public class FormattingDates  {
            
                public static void main(String[] args) {
                  System.out.printf("Right now it is %tr on " +
                                    "%<tA, %<tB %<te, %<tY.%n",
                                    Calendar.getInstance());
                }
              }
           //说明:“<”指示采用的参数为前一个被格式化的参数
           //输出
           Right now it is 01:55:19 PM on Wednesday, September 22, 2004.
            
          

       
      



      
      
       
       

         
       

         
       
      


      



                            function TempSave(ElementID)
                            {
                                    CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
                                    CommentsPersistDiv.save("CommentXMLStore");
                            }
                            function Restore(ElementID)
                            {
                                    CommentsPersistDiv.load("CommentXMLStore");
                                    document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
                            }
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-2 09:36 , Processed in 0.376092 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表