Formatted output for Java

Source: Internet
Author: User

formatted output for Java

A method to format the output requires a format string and a parameter list (arguments). The format string may include the determined text (fixed text) and one or more format specifiers (formatted specifier). Look at the following example:

String S=string.format ("Duke ' s Name:%s", Name);//format (string format,object arguments)

This is the format method of the Java string class, in which "Duke's Name:%s" is the first parameter of this method, that is, the format string, where (%s) is the format specifier, except that the remainder is the determined text. Name is the parameter list (arguments).

for the general case, the syntax for the format specifier is as follows :

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

• The format specifier starts with a% symbol and is replaced with the corresponding parameter. (% symbol can be based on \ Understand, to output the% symbol, the format needs to write such a percent)

Argument_index is a decimal integer, as the name implies, that represents the position of the following argument, such as the 1$ above for name.

flags is used to control the output format, but specifically how to control the end of the conversion (conversion).

width is a positive integer that indicates the length of the character to be output.

Conversion character

Sign

Code:

Public class formatdemo {

Public static void main (string[] args) {

// TODO auto-generated method stub

System. out. println ("Hello World");

Double x=-10000.0/3.0;

double y=5000.0/3.0;

System. out. println (x);

System. out. printf ("%,10.2f\r\n",x);

System. out. printf ("%-,10.2f\r\n",x);

         system. out .printf ( "%+ (, 10.2f%2$.3f\r\n" ,x , y

System. out. printf ("%+ (, 10.2f%1$.3f%2$.3f%<f%<.3f\r\n",x,y);

    }

}

• Results:

• Date format conversion:

The format specifier feature of the date is that the conversion in the descriptor is started by T, plus any character in the following table ends.

Time is expressed as a number of milliseconds (positively negative) from a fixed point in time, which is the so-called epoch (epoch), which is the abbreviation for UTC time of January 1, 1970 00:00:00.UTC is coordinated Universal. Same as GMT (Greenwich Mean time/GMT). Java separates the saved time from the name of the point-in-time. The date class is used to represent the point in time, and the other is the Hitachi notation Calendar class.

From "Java Core technology Volume I"

Formatted output for Java

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.