Java.math.BigDecimal.toBigInteger() Method


Advertisements


Description

The java.math.BigDecimal.toBigInteger() converts this BigDecimal to a BigInteger. This conversion is analogous to the narrowing primitive conversion from double to long. Any fractional part of this BigDecimal will be discarded. This conversion can lose information about the precision of the BigDecimal value.

To have an exception thrown if the conversion is inexact (in other words if a nonzero fractional part is discarded), use the toBigIntegerExact() method.

Declaration

Following is the declaration for java.math.BigDecimal.toBigInteger() method.

public BigInteger toBigInteger()

Parameters

NA

Return Value

This method returns the value of BigDecimal object converted to a BigInteger.

Exception

NA

Example

The following example shows the usage of math.BigDecimal.toBigInteger() method.

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 );
   }
}

Let us compile and run the above program, this will produce the following result −

BigInteger value of 235.738 is 235

java_math_bigdecimal.htm

Advertisements