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


Java Java.math.BigDecimal.toBigInteger()用法及代碼示例


描述

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

要在轉換不準確時拋出異常(換句話說,如果丟棄了非零小數部分),請使用 toBigIntegerExact() 方法。

聲明

以下是聲明java.math.BigDecimal.toBigInteger()方法。

public BigInteger toBigInteger()

參數

NA

返回值

此方法返回轉換為 BigInteger 的 BigDecimal 對象的值。

異常

NA

示例

下麵的例子展示了 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.toBigInteger() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。