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 

引數

  • NA

返回值

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

異常

  • NA

例子

下面的例子顯示math.BigInteger.intValue()方法的用法

package com.yiibai;

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