Java.math.BigInteger.intValue() 方法



描述

java.math.BigInteger.intValue() 將此 BigInteger 轉換為 int。此轉換類似於從 long 到 int 的浮點原始轉換。

如果此 BigInteger 過大而無法容納在 int 中,則僅返回低階 32 位。此轉換可能會丟失有關 BigInteger 值的總體大小的資訊,並返回結果為相反符號。

宣告

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

public int intValue()

規定

Number 類中的 intValue。

引數

返回值

此方法返回此 BigInteger 轉換為 int 的結果。

異常

示例

以下示例演示了 math.BigInteger.intValue() 方法的使用。

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger bi1, bi2;

      // create 2 Integer objects
      Integer i1, i2;

      // assign values to bi1, bi2
      bi1 = new BigInteger("123");
      bi2 = new BigInteger("9888486986");

      // assign the integer values of bi1, bi2 to i1, i2
      i1 = bi1.intValue();
      i2 = bi2.intValue();

      String str1 = "Integer value of " +bi1+ " is " +i1;
      String str2 = "Integer value of " +bi2+ " is " +i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

我們編譯並執行以上程式,這會生成以下結果 -

Integer value of 123 is 123
Integer value of 9888486986 is 1298552394
java_math_biginteger.htm
廣告
© . All rights reserved.