當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.math.BigInteger.intValue()用法及代碼示例


描述

這個java.math.BigInteger.intValue()將此 BigInteger 轉換為 int。這種轉換類似於從 long 到 int 的縮小原始轉換。

如果此 BigInteger 太大而無法放入 int,則僅返回低 32 位。此轉換可能會丟失有關 BigInteger 值的整體大小的信息,並返回具有相反符號的結果。

聲明

以下是聲明java.math.BigInteger.intValue()方法。

public int intValue()

指定者

類中的 intValueNumber

參數

NA

返回值

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

異常

NA

示例

下麵的例子展示了 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.intValue() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。