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
廣告