Java.math.BigInteger.longValue() 方法



說明

java.math.BigInteger.longValue() 將 BigInteger 轉換為一個 long。這種轉換類似於從 long 到 int 的窄基本型別轉換。

如果 BigInteger 過大以至於無法用 long 容納,則僅返回低位 64 位。這種轉換可能會丟失 BigInteger 值的總體大小方面的相關資訊,並且返回帶相反符號的結果。

宣告

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

public long longValue()

規定於

數字類中的 longValue。

引數

NA

返回值

該方法返回 BigInteger 轉換為 long。

異常

NA

示例

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

package com.tutorialspoint;

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
java_math_biginteger.htm
廣告
© . All rights reserved.