New formatted output of JDK5 new features

Source: Internet
Author: User
Tags exit locale new features printf string format java format
JDK5.0 allows you to format the output directly using the printf () method as C, and provides a number of parameters to format the input, and the invocation is simple:

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

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

The printf () and format () methods have the same functionality. System.out is an example 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 more sophisticated way to format, for example:

Formatter.format ("Pi is approximately%1$f," +

"and E is about%2$f", Math.PI, MATH.E);


The formatting elements are composed as follows:

%[argument_index$][flags][width][.precision]conversion

which

Argument_index is a positive integer that shows the position of the parameter, 1 is the first argument

Width represents the minimum number of letters for the output

Precision represents the number of decimal places

Conversion represents the type of the parameter being formatted:

F Float,

T time

D Decimal

o Octal

x hexadecimal

S General

c a Unicode character

Here's 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 Regional Settings

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);

}

}

Console call

Java format/usingformatter%.2f

Output

Pi is approximately 3,14, and e are about 2,72

Using format,printf to replace the method

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);

}

}

Console call

Java format/usingsystemout%.2f%n

Output

Pi is approximately 3.14

, and E is about 2.72

The format of time is represented by the letter T, which usually follows the T with a special character to show 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's%tr on" +

"%<ta,%<TB%<te,%<ty.%n",

Calendar.getinstance ());

}

}

Note: "<" indicates that the parameter used is the previous formatted parameter

Output

Right now it's 01:55:19 PM on Wednesday, September 22, 2004.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.