Let's take a look at another new stuff. . . JDK5.0 allows you to format the output with a printf () method as in C language, and provide many parameters to format the input, and the call is also simple:
System.Out.Format ("pi is approximately% f", math.pi); system.out.printf ("pi is approximately% f", Math.pi); printf () and format () method have the same function System.out is an instance of java.io.printStream. PrintStream, java.io.printwriter, and java.lang.String Each class has four new formatting methods:
Format (String Format, Object ... ARGS); Printf (String Format, Object ... ARGS); Format (Locale Locale, String Format, Object ... Args); Printf (locale locale, string format, object .. ARGS);
At the same time, the previous Formatter class also provides a better way to format, for example:
Formatter.Format ("Pi is approximately% 1 $ f," "and e is about% 2 $ f", Math.pi, Math.e;
The composition of the formatting element is as follows:
% [Argument_index $] [Flags] [width] [. Precision] Conversion
Where: argument_index is a positive integer, illustrating the location of the parameters, 1 is taken to take the first parameter
Width represents the number of minimum letters output
Precision represents the number of decimal numbers
Conversion represents the type of parameters formatted:
f float,
t time
D Decimal
o Octal
X Hexadecimal
S General
C a Unicode Character
The following is an example:
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);
}
}
// Console call
Java format / usingformatter% f
// output
Pi is approximately 3.141593, and e is about 2.718282
// Console call
Java format / usingformatter% .2f // output
Pi is Approximately 3.14, And E Is About 2.72
// Console call
Java format / usingformatter% 6.2F
// Output (with space to fill the length)
Pi is Approximately 3.14, And E Is About 2.72
// Console call
Java format / usingformatter% 1 $ .2F
// output
Pi Is Approximately 3.14, And E Is About 3.14
// Change the area setting
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
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);
}
}
// Console call
Java format / usingformatter% .2f
// output
Pi Is Approximately 3,14, And E Is About 2,72
// Use Format, PrintF alternative ways
Package format;
Public class usingsystemout {
Public static void main (String [] args) {
IF (args.length! = 1) {
System.err.println ("USAGE:"
"Java Format / Usingsystemout
System.exit (0);
}
String Format = args [0];
System.out.Format ("Pi Is Approximately" Format
", and e is approximately" format, math.pi, math.e;
}
}
// Console call
Java format / usingsystemout% .2f% N
// output
Pi is approximately 3.4
And e is about 2.72
The formatting of time is represented by the letter T, which is usually coupled with a special character to display a part of the time:
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"
"% Calendar.getInstance ()); } } // Description: "<" indicates the parameters used as the previously formatted parameters. // output Right now it is 01:55:19 PM ON Wednesday, September 22, 2004.