Java.math.BigDecimal.toBigIntegerExact() Method

Advertisements


Description

The java.math.BigDecimal.toBigIntegerExact() converts this BigDecimal to a BigInteger, checking for lost information. An exception is thrown if this BigDecimal has a nonzero fractional part.

Declaration

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

public BigInteger toBigIntegerExact()

Parameters

  • NA

Return Value

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

Exception

  • ArithmeticException - if this has a nonzero fractional part

Example

The following example shows the usage of math.BigDecimal.toBigIntegerExact() 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("2426");

        // assign the BigIntegerExact value of bg1 to i1
        i1 = bg1.toBigIntegerExact();

	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 2426 is 2426



Advertisements
Advertisements