Java.math.BigInteger.toString() 方法



說明

java.math.BigInteger.toString(int radix) 返回此 BigInteger 在給定基數中的字串表示形式。如果基數超出了 Character.MIN_RADIX 到 Character.MAX_RADIX(含)的範圍,則將預設為 10(與 Integer.toString 的情況相同)。

使用 Character.forDigit 提供的數字到字元對映,並在適當的情況下新增一個減號。此表示形式與此(字串、int)建構函式相容。

宣告

以下是 java.math.BigInteger.toString() 方法的宣告。

public String toString(int radix)

引數

radix - 字串表示形式的基數。

返回值

此方法返回此 BigInteger 在給定基數中的字串表示形式。

異常

不適用

示例

以下示例顯示了 math.BigInteger.toString() 方法的用法。

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger bi1, bi2;

      // create 2 String objects
      String s1, s2;

      bi1 = new BigInteger("16");
      bi2 = new BigInteger("-16");

      // assign String value of bi1, bi2 to s1, s2 using radix
      s1 = bi1.toString(8); // octal value of bi1
      s2 = bi2.toString(2); // binary value of bi2

      String str1 = "String value of " +bi1+ " in radix 8 is " +s1;
      String str2 = "String value of " +bi2+ " in radix 2 is " +s2;

      // print s1, s2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

讓我們編譯並執行以上程式,這將產生以下結果 -

String value of 16 in radix 8 is 20
String value of -16 in radix 2 is -10000
java_math_biginteger.htm
廣告
© . All rights reserved.