Java.math.BigDecimal.toBigInteger() 方法



描述

java.math.BigDecimal.toBigInteger() 將此 BigDecimal 轉換為 BigInteger。此轉換類似於從 double 到 long 的原始縮小轉換。此 BigDecimal 的任何小數部分都將被丟棄。此轉換可能會丟失有關 BigDecimal 值精度的資訊。

如果轉換不精確(換言之,如果丟棄非零的小數部分),則使用 toBigIntegerExact() 方法丟擲異常。

宣告

以下是 java.math.BigDecimal.toBigInteger() 方法的宣告。

public BigInteger toBigInteger()

引數

不適用

返回值

此方法返回轉換為 BigInteger 的 BigDecimal 物件的值。

異常

不適用

示例

以下示例展示了 math.BigDecimal.toBigInteger() 方法的用法。

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create a BigDecimal object
      BigDecimal bg1;

      // create a BigInteger object
      BigInteger i1;

      bg1 = new BigDecimal("235.738");

      // assign the BigInteger value of bg1 to i1
      i1 = bg1.toBigInteger();

      String str = "BigInteger value of " + bg1 + " is " + i1;

      // print i1 value
      System.out.println( str );
   }
}

讓我們編譯並執行上述程式,這會產生以下結果 −

BigInteger value of 235.738 is 235
java_math_bigdecimal.htm
廣告