JDK5 new features: Formatted output __JDK

Source: Internet
Author: User
Tags locale string format java format

DK5.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.

 

 

syntax for format Strings

Each method that produces the formatted output requires a format string and a list of parameters. A format string is a string that can contain fixed text and one or more embedded format specifiers. Consider the following example:

Calendar C = ...;

String s = String.Format ("Duke ' s Birthday:%1$tm%1$te,%1$ty", c);

This format string is the first parameter of the format method. It contains three format specifiers, "%1$tm", "%1$te", and "%1$ty", which indicate how parameters should be handled and where they are inserted in the text. The remainder of the format string is a fixed text that includes "Dukes Birthday:" And any other space or punctuation. The argument list consists of all the parameters passed to the method that is located after the format string. In the example above, the argument list has a size of 1 and consists of Calendar of new objects.

The syntax for format specifiers for general types, character types, and numeric types is as follows:

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

The optional Argument_index is a decimal integer that indicates the position of the parameter in the argument list. The first parameter is referenced by "1$", the second parameter is referenced by "2$", and so on.

The optional flags are the character sets that modify the output format. The collection of valid flags depends on the type of conversion.

The optional width is a nonnegative decimal integer indicating the minimum number of characters to write to the output.

An optional precision is a nonnegative decimal integer that is typically used to limit the number of characters. Specific behavior depends on the type of conversion.

The required conversion is a character that indicates how the parameter should be formatted. The set of valid conversions for a given parameter depends on the data type of the parameter.

The syntax for formatting specifiers that represent date and time types is as follows:

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

The optional Argument_index, flags, and width definitions are as above.

The desired conversion is a sequence of two characters. The first character is ' t ' or ' t '. The second character indicates the format used. These characters are similar but not exactly equivalent to those defined by GNU date and POSIX strftime (3c).

The syntax for the format specifier that does not correspond to the parameter is as follows:

%[flags][width]conversion

Optional flags and width are defined as above.

The desired conversion is a character that indicates the content to be inserted in the output.

Transformation

Conversions can be grouped into the following categories:

General-can be applied to any parameter type

Characters-can be applied to basic types that represent Unicode characters: char, Character, Byte, Byte, short, and short. When character.isvalidcodepoint (int) Returns TRUE, this transformation can be applied to int and Integer types

Numerical

Integers-integer types that can be applied to Java: Byte, Byte, short, short, int, integer, long, long, and BigInteger

Floating point-floating-point types available for Java: float, float, double, double, and BigDecimal

Date/Time-a type that can be applied to Java to encode a date or time: Long, long, Calendar, and date.

Percent-produce literal '% ' ('/u0025 ')

Row separator-produces platform-specific row delimiters

The following table summarizes the supported transformations. By uppercase characters, such as

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.