Java.math.BigInteger.longValue()方法範例


java.math.BigInteger.longValue() 將此BigInteger轉換為long。這種轉換是類似於從一個較長縮小原語轉換為int。

如果此BigInteger是太大,不適合太長的,只有低64位元被返回。此轉換會丟失關於BigInteger值的總大小資訊以及符號相反返回結果。

宣告

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

public long longValue()

Specified by

Number 類中的longValue

引數

  • NA

返回值

此方法返回此BigInteger轉換為 long。

異常

  • NA

例子

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

package com.yiibai;

import java.math.*;

public class BigIntegerDemo {

public static void main(String[] args) {

	// create 2 BigInteger objects
	BigInteger bi1, bi2;

	// create 2 Long objects
	Long l1, l2;

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

	// assign the long values of bi1, bi2 to l1, l2
	l1 = bi1.longValue();
	l2 = bi2.longValue();

	String str1 = "Long value of " +bi1+ " is " +l1;
	String str2 = "Long value of " +bi2+ " is " +l2;

	// print l1, l2 values
	System.out.println( str1 );
	System.out.println( str2 );
    }
}

讓我們編譯和執行上面的程式,這將產生以下結果:

Long value of -123 is -123
Long value of 9888486986 is 9888486986