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
广告
© . All rights reserved.