printf()方法是用于格式化的方法。
2,源代码
public class Main {
public static void main(String[] args) {
System.out.printf("%d This is a format String ", 123);
boolean myBoolean = true;
char myChar = '@';
String myString = "hiro";
int myInt = 50;
double myDouble = 1000;
System.out.printf("%b", myBoolean);
System.out.printf("%c", myChar);
System.out.printf("%s", myString);
System.out.printf("%d", myInt);
System.out.printf("%f", myDouble);
System.out.printf("Hello %10s", myString); //
System.out.printf("Hello %-10s", myString); //
System.out.printf("You have this much money %.2f", myDouble);
System.out.printf("You have this much money %.1f", myDouble);
System.out.printf("You have this much money %f", myDouble);
System.out.printf("You have this much money %+f", myDouble);
System.out.printf("You have this much money %020f", myDouble);
System.out.printf("You have this much money %,f", myDouble);
}
}
Java 教程