在 Java 中解析和格式化一個大整數到八進位制
首先,建立兩個 BigInteger 物件並設定值。
BigInteger one, two;
one = new BigInteger("99");現在,將 BigInteger 物件“two”解析為八進位制形式。
two = new BigInteger("1100", 8);
String str = two.toString(8);以上我們使用了以下建構函式。此處,radix 設定為 8,表示八進位制。對於 BigInteger 建構函式和 toString() 方法都是如此。
BigInteger(String val, int radix)
此建構函式用於將指定基數中 BigInteger 的字串表示形式轉換為 BigInteger。
以下是一個示例 -
示例
import java.math.*;
public class Demo {
public static void main(String[] args) {
BigInteger one, two;
one = new BigInteger("99");
// parsing BigInteger object "two" into Octal
two = new BigInteger("1100", 8);
String str = two.toString(8);
System.out.println("Result (BigInteger) : " +one);
System.out.println("Result after parsing : " +str);
}
}輸出
Result (BigInteger) : 99 Result after parsing : 1100
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP