A summary of some common print formats for Java printf

Source: Internet
Author: User
Tags print format java printf

Package basedemo1;/** * printf Some common print format summary * @author Chen November 27, 2014 16:26:07 * */public class Printfdemo {public static V OID Main (string[] args) {String str = "I like data mining"; int I = 1234;double D = 3.14159265358979;float f1 = -0.123456f; float F2 = 0.123456f; System.out.printf ("1,str =%20s\n", str); String type System.out.printf ("2,i =%d\n", i); int type System.out.printf ("3,d =%f\n", d); Double type, the default decimal number is 6 bits/*10 indicates the length of the output, from right to left all numbers and the length of the character, if the character does not have 10, then the left side of the upper margin, if more than 10, the characters are all printed out */system.out.printf ("4,d =%10f\n" , d); System.out.printf ("5,d =%.2f\n", d); .2 represents the number of decimal places System.out.printf ("6,F1 =%f\n", F1); Flaot type System.out.printf ("7,F2 =%f\n", f2); Float type, when positive, unsigned System.out.printf ("8,F2 =%+f\n", f2); Float type, when positive, displays the symbol of the number System.out.printf ("9,%%"); Print "%", write only one% will error System.out.println (); System.out.println (""); Printfdemo.ninenine ();} /** * Description: Print 99 multiplication table * */static void Ninenine () {for (int i = 1; i < i++) {for (int j = 1; J <= I; j + +) {Syste m.out.printf ("%d*%d =%2d", I, J, I*j); }system.out.println ();}}}

Printing results:

1,str =   I like data mining2,i = 12343,d = 3.1415934,d =   


A summary of some common print formats for Java printf

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.