Java.math.BigInteger.doubleValue() 方法



描述

java.math.BigInteger.doubleValue() 將此 BigInteger 轉換為 double。此轉換類似於從 double 到 float 的強制原始轉換。

如果此 BigInteger 的大小太大而無法表示為 double,它將被轉換為 Double.NEGATIVE_INFINITY 或 Double.POSITIVE_INFINITY(視情況而定)。即使返回值有限,此轉換也會丟失有關 BigInteger 值精度的資訊。

宣告

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

public double doubleValue()

指定者

Number 類中的 doubleValue。

引數

返回值

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

異常

範例

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger bi1, bi2;

      // create 2 Double objects
      Double d1, d2;

      // assign value to bi1
      bi1 = new BigInteger("123");

      // assign a larger value to bi2
      bi2 = new BigInteger("12345678");

      // assign double value of bi1, bi2 to d1, d2
      d1 = bi1.doubleValue();
      d2 = bi2.doubleValue();

      String str1 = "Double value of " + bi1 + " is " +d1;
      String str2 = "Double value of " + bi2 + " is " +d2;

      // print d1, d2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

線上演示

Double value of 123 is 123.0
Double value of 12345678 is 1.2345678E7
讓我們編譯並執行上述程式,這將產生以下結果 -
列印頁面
廣告
© . All rights reserved.