1 Packagetest; 2 3 4 Public classMain5 { 6 Public Static voidMain (string[] args)7 { 8 //define some variables that you can use to format the output. 9 DoubleD = 345.678; TenString s = "Hello!" "; One inti = 1234; A //"%" means the formatted output, and the content after "%" is defined as the format. -System.out.printf ("%f", D);//"F" indicates the formatted output floating-point number. - System.out.println (); theSystem.out.printf ("%9.2f", D);//9 in "9.2" indicates the length of the output, and 2 indicates the number of digits after the decimal point. - System.out.println (); -System.out.printf ("%+9.2f", D);//"+" indicates the number of positive and negative numbers for the output. - System.out.println (); +System.out.printf ("%-9.4f", D);//"-" indicates the left-justified number of outputs (default is right-aligned). - System.out.println (); +System.out.printf ("%+-9.3f", D);//"+-" indicates the number of positive and left-aligned outputs. A System.out.println (); atSystem.out.printf ("%d", I);//"D" indicates an output decimal integer. - System.out.println (); -System.out.printf ("%o", I);//"O" indicates the output octal integer. - System.out.println (); -System.out.printf ("%x", I);//"D" indicates an output hexadecimal integer. - System.out.println (); inSystem.out.printf ("% #x", i);//"D" indicates the output of an integer with a hexadecimal flag. - System.out.println (); toSystem.out.printf ("%s", s);//"D" represents the output string. + System.out.println (); -System.out.printf ("Output a floating-point number:%f, an integer:%d, a string:%s", D, I, s); the //You can output multiple variables and note the order. * System.out.println (); $System.out.printf ("string: Hexadecimal number of%2$s,%1$d:%1$ #x", I, s); Panax Notoginseng //"x$" represents the first few variables. - } the}
How to use Java system.out.printf ()