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