在 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

更新日期: 2020-6-29

227 次瀏覽

開啟你的 職業生涯

完成該課程,獲得認證

開始學習
廣告
© . All rights reserved.