Java 中八进制和十六进制的格式说明符
对于 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP