Java 打印支持两种类型的数字格式:
整数格式化格式化整数。
它可以格式化byte, Byte, short, Short, int, Integer, long, Long和BigInteger的值。
以下列表说明了格式字符。
dox或大写变量X用于整数格式化的格式说明符的一般语法如下:
%<argument_index$><flags><width><conversion>
格式说明符的精度不适用于整数格式化。
下面的代码演示了使用带有各种标志的“d"转换来格式化整数:
public class Main {
public static void main(String[] args) {
System.out.printf(""%d" %n", 1234);
System.out.printf(""%6d" %n", 1234);
System.out.printf(""%-6d" %n", 1234);
System.out.printf(""%06d" %n", 1234);
System.out.printf(""%(d" %n", 1234);
System.out.printf(""%(d" %n", -1234);
System.out.printf(""% d" %n", 1234);
System.out.printf(""% d" %n", -1234);
System.out.printf(""%+d" %n", 1234);
System.out.printf(""%+d" %n", -1234);
}
}
上面的代码生成以下结果。

当 o 和 x 与byte, Byte, short, Short, int, Integer, long, 和 Long 数据类型的负参数一起使用时,参数值首先通过向其加上数字2N转换为无符号数, 是值的位数。 转换不会转换负BigInteger参数。
public class Main {
public static void main(String[] args) {
byte b1 = 1;
byte b2 = -2;
System.out.printf("%o\n", b1);
System.out.printf("%o", b2);
}
}

以下代码段显示了int和BigInteger的“o"和“x"转换的更多示例参数类型:
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
System.out.printf("%o %n", 1234);
System.out.printf("%o %n", -1234);
System.out.printf("%o %n", new BigInteger("1234"));
System.out.printf("%o %n", new BigInteger("-1234"));
System.out.printf("%x %n", 1234);
System.out.printf("%x %n", -1234);
System.out.printf("%x %n", new BigInteger("1234"));
System.out.printf("%x %n", new BigInteger("-1234"));
System.out.printf("%#o %n", 1234);
System.out.printf("%#x %n", 1234);
System.out.printf("%#o %n", new BigInteger("1234"));
System.out.printf("%#x %n", new BigInteger("1234"));
}
}
上面的代码生成以下结果。

浮点数字格式处理整数部分和数字的分数部分。
浮点数格式化可应用于float,Float,double,Double和BigDecimal数据类型的格式值。
以下列表包含用于格式化浮点数的转换。
e和大写变量Eg和大写变量Gfa和大写变量A浮点数格式化的一般语法是
%<argument_index$><flags><width><.precision><conversion>
精度具有不同的含义取决于转换字符。
以下代码段显示了如何使用默认精度(6为格式)格式化浮点数:
public class Main {
public static void main(String[] args) {
System.out.printf("%e %n", 10.2);
System.out.printf("%f %n", 10.2);
System.out.printf("%g %n", 10.2);
System.out.printf("%e %n", 0.000001234);
System.out.printf("%f %n", 0.000001234);
System.out.printf("%g %n", 0.000001234);
System.out.printf("%a %n", 0.000001234);
}
}
上面的代码生成以下结果。

以下代码显示了在浮点数格式中使用width和precision的影响:
public class Main {
public static void main(String[] args) {
System.out.printf("%.2e %n", 123456.789);
System.out.printf("%.2f %n", 123456.789);
System.out.printf("%.2g %n", 123456.789);
System.out.printf(""%8.2e" %n", 123456.789);
System.out.printf(""%8.2f" %n", 123456.789);
System.out.printf(""%8.2g" %n", 123456.789);
System.out.printf(""%10.2e" %n", 123456.789);
System.out.printf(""%10.2f" %n", 123456.789);
System.out.printf(""%10.2g" %n", 123456.789);
System.out.printf(""%-10.2e" %n", 123456.789);
System.out.printf(""%-10.2f" %n", 123456.789);
System.out.printf(""%-10.2g" %n", 123456.789);
System.out.printf(""%010.2e" %n", 123456.789);
System.out.printf(""%010.2f" %n", 123456.789);
System.out.printf(""%010.2g" %n", 123456.789);
}
}
上面的代码生成以下结果。

如果浮点转换的参数值为NaN或Infinity,则输出分别包含字符串“NaN”和“Infinity”。
public class Main {
public static void main(String[] args) {
System.out.printf("%.2e %n", Double.NaN);
System.out.printf("%.2f %n", Double.POSITIVE_INFINITY);
System.out.printf("%.2g %n", Double.NEGATIVE_INFINITY);
System.out.printf("%(f %n", Double.POSITIVE_INFINITY);
System.out.printf("%(f %n", Double.NEGATIVE_INFINITY);
}
}
上面的代码生成以下结果。

© 著作权归作者所有