one ,System.out.printf()
Java SE5 Launched C language printf() Style of formatted output function .
String str="Java"; double pi=3.14; int i=100; //"%" Represents to format output , Then there is the definition of format System.
out.printf("%f\n",pi);//"f" Represents a formatted output floating-point number System.out.printf("%d\n",i);
//"d" Represents the formatted output decimal integer System.out.printf("%o\n",i);//"o" Represents a formatted output octal integer System.out.
printf("%x\n",i);//"x" Represents a formatted output hexadecimal integer System.out.printf("%s\n",str);
//"s" Represents a formatted output string System.out.printf(" A string :%s, A floating point number :%f, An integer :%d",str,pi,i);
// You can output more than one variable at a time , Just pay attention to the order
two ,System.out.format()
Java
SE5 Introduced format() Method imitation C Of printf() method , Can be used for PrintStream perhaps PrintWriter object , include System.out object . The usage is basically the same System.out.printf() similar .
String str="Java"; double pi=3.14; int i=100; //"%" Represents to format output , Then there is the definition of format System.
out.format("%f\n",pi);//"f" Represents a formatted output floating-point number System.out.format("%d\n",i);
//"d" Represents the formatted output decimal integer System.out.format("%o\n",i);//"o" Represents a formatted output octal integer System.out.
format("%x\n",i);//"x" Represents a formatted output hexadecimal integer System.out.format("%s\n",str);
//"s" Represents a formatted output string System.out.format(" A string :%s, A floating point number :%f, An integer :%d",str,pi,i);
// You can output more than one variable at a time , Just pay attention to the order
three ,Fomatter class
Java All formatting functions in are provided by java.util.Formatter Class processing . When you create a Formatter Object
, You need to pass some information to its constructor , Tell it where the final result will go .
import java.util.Formatter;// use Formatter Class needs to be imported java.util.Formatter Formatter f=
new Formatter(System.out);// Create a Formatter object , Specifies that the output is System.out String str="Java";
double pi=3.14; int i=100; //"%" Represents to format output , Then there is the definition of format f.format("%f\n",pi);
//"f" Represents a formatted output floating-point number f.format("%d\n",i);//"d" Represents the formatted output decimal integer f.format("%o\n",i);
//"o" Represents a formatted output octal integer f.format("%x\n",i);//"x" Represents a formatted output hexadecimal integer f.format("%s\n",str);
//"s" Represents a formatted output string f.format(" A string :%s, A floating point number :%f, An integer :%d",str,pi,i);
// You can output more than one variable at a time , Just pay attention to the order
four ,String.format()
String.format() It's a static method , Reception and Formatter.format() Same parameters , Its return value :String object , It is suitable for primary output .
String str="Java"; double pi=3.14; int i=100; //"%" Represents to format output , Then there is the definition of format System.
out.println(String.format("%f",pi));//"f" Represents a formatted output floating-point number System.out.println(String.
format("%d",i));//"d" Represents the formatted output decimal integer System.out.println(String.format("%o",i));
//"o" Represents a formatted output octal integer System.out.println(String.format("%x",i));//"x" Represents a formatted output hexadecimal integer
System.out.println(String.format("%s",str));//"s" Represents a formatted output string
Technology