BigDecimal toBigInteger() Method in Java
The java.math.BigDecimal.toBigInteger() is an inbuilt method in java that 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.
Note: If an exception is thrown in the conversion inexact (in other words if a nonzero fractional part is discarded), use the toBigIntegerExact() method.
Syntax:
public BigInteger toBigInteger()
Parameters: This method does not accepts any parameters.
Return value: This method returns the value of BigDecimal object converted to a BigInteger.
Examples:
Input: (BigDecimal) 123.321
Output: (BigInteger) 123
Input: (BigDecimal) 123.001
Output: (BigInteger) 123
Below programs illustrate the working of the above-mentioned method:
Program 1:
Java
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigDecimal b = new BigDecimal( "123.321" );
BigInteger i = b.toBigInteger();
System.out.println( "BigInteger value of " + b + " is " + i);
}
}
|
Output:
BigInteger value of 123.321 is 123
Program 2:
Java
import java.math.*;
public class GFG {
public static void main(String[] args)
{
BigDecimal b = new BigDecimal( "123.001" );
BigInteger i = b.toBigInteger();
System.out.println( "BigInteger value of " + b + " is " + i);
}
}
|
Output:
BigInteger value of 123.001 is 123
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#toBigInteger()
Last Updated :
09 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...