对于Formatter,请导入以下包-
import java.util.Formatter;
现在创建一个这样的Formatter对象-
Formatter f1 = new Formatter(); Formatter f2 = new Formatter(); Formatter f3 = new Formatter();
如果您想要八进制的格式说明符,请使用%o-
f3.format("Octal values %o %o %o", 15, 55, 78);如果您想要十六进制的格式说明符,请使用%x-
f2.format("Hexadecimal values %x %x %x", 24, 98, 110);以下是一个例子-
import java.util.Formatter;
public class Demo {
public static void main(String args[]) {
Formatter f1 = new Formatter();
Formatter f2 = new Formatter();
Formatter f3 = new Formatter();
f1.format("Rank and Percentage of %s = %d %f", "David", 2, 98.5);
System.out.println(f1.toString());
f2.format("Hexadecimal values %x %x %x", 24, 98, 110);
System.out.println(f2.toString());
f3.format("Octal values %o %o %o", 15, 55, 78);
System.out.println(f3.toString());
}
}输出结果
Rank and Percentage of David = 2 98.500000 Hexadecimal values 18 62 6e Octal values 17 67 116