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


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


描述

這個java.math.BigInteger.doubleValue()將此 BigInteger 轉換為雙精度值。這種轉換類似於從 double 到 float 的縮小原始轉換。

如果此 BigInteger 的量級太大而無法表示為雙精度數,則將根據需要將其轉換為 Double.NEGATIVE_INFINITY 或 Double.POSITIVE_INFINITY。即使返回值是有限的,這種轉換也會丟失有關 BigInteger 值精度的信息。

聲明

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

public double doubleValue()

指定者

類中的雙值Number

參數

NA

返回值

此方法返回轉換為雙精度的 BigInteger。

異常

NA

示例

下麵的例子展示了 math.BigInteger.doubleValue() 方法的用法。

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

相關用法


注:本文由純淨天空篩選整理自 Java.math.BigInteger.doubleValue() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。