[Java的] 在Java中一些格式 – 格式化Java中的数字打印输出
在Java报价 2 在我们的格式化方法 的printf 和 格式 和 2 该方法具有类似的功能.
随着 1 变量可以以不同的方式与它被格式化. 首席执行官
System.out.format("The value of " + "the float variable is " +
"%f, while the value of the " + "integer variable is %d, " +
"and the string is %s", floatVar, intVar, stringVar);
int i = 461012;
System.out.format("The value of i is: %d%n", i);
另一件事是,你可以根据法国系统打印格式, 分离的整数部分和实数的十进制格式的小数部分 Locale.FRANCE. 首席执行官:
System.out.format(Locale.FRANCE,
"The value of the float " + "variable is %f, while the " +
"value of the integer variable " + "is %d, and the string is %s%n",
floatVar, intVar, stringVar);
我有一个表格式:

你可以运行一个简单的程序:
import java.util.Calendar;
import java.util.Locale;
public class FormattingNumericPrintOutput {
public static void main(String[] args) {
long n = 461012;
System.out.format("%d%n", n); // --> "461012"
System.out.format("%08d%n", n); // --> "00461012"
System.out.format("%+8d%n", n); // --> " +461012"
System.out.format("%,8d%n", n); // --> " 461,012"
System.out.format("%+,8d%n%n", n); // --> "+461,012"
double pi = Math.PI;
System.out.format("%f%n", pi); // --> "3.141593"
System.out.format("%.3f%n", pi); // --> "3.142"
System.out.format("%10.3f%n", pi); // --> " 3.142"
System.out.format("%-10.3f%n", pi); // --> "3.142"
System.out.format(Locale.FRANCE, "%-10.4f%n%n", pi); // --> "3,1416" //in theo hệ thống Pháp
Calendar c = Calendar.getInstance(); //lấy thời gian hiện hành
System.out.format("%tB %te, %tY%n ", c, c, c); // --> "July 20, 2013"
System.out.format("%tl:%tM %tp%n", c, c, c); // --> " 2:51 pm"
System.out.format("%tD%n", c); // --> "07/20/13"
}
}



如果数字的类型为double,则格式与根据floatàanh完全相同?