惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
腾讯CDC
aimingoo的专栏
aimingoo的专栏
月光博客
月光博客
Engineering at Meta
Engineering at Meta
C
Check Point Blog
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
WordPress大学
WordPress大学
博客园 - 司徒正美

博客园 - fengye515

开发常用工具 Qt的内存管理 pyqt4制作透明无边框窗体 面向对象 Java 1.5新特性Enum的用法 Java线程学习和总结 JVM,反射与动态代理 JDK5新特性之一----静态导入 第五章Java虚拟机(Chapter Five: The Java Virtual Machine) Java虚拟机体系结构概述 Java虚拟机 面向对象的思维方法 JDK5.0的11个主要新特征 精通java的一些必要条件 简单的范型实例程序 Spring 入门 Hibernate快速入门 设计模式 用J2ME开发企业级无线应用
JDK5新特性之二----新的格式化输出
fengye515 · 2007-02-18 · via 博客园 - fengye515

JDK5.0允许象C语言那样直接用printf()方法来格式化输出,并且提供了许多参数来格式化输入,调用也很简单:

System.out.format("Pi is approximately %f", Math.Pi);

   System.out.printf("Pi is approximately %f", Math.Pi);

 printf()和 format() 方法具有相同的功能. System.outjava.io.PrintStream的实例. PrintStream, java.io.PrintWriter, 和 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代表被格式化的参数的类型:

f float,

t time

d decimal

o octal

x hexadecimal

s general

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$.2f

//输出

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,

tA the day of the week

tB the name of the month

te the number of the day of the month

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.

posted @ 2007-02-18 23:29  fengye515  阅读(300)  评论()    收藏  举报