Java: Convert from BigDecimal to BigInteger

Drop Fractional Part (12.99 → 12)

Use BigDecimal.toBigInteger:

BigDecimal bigDec = ...
BigInteger bigInt = bigDec.toBigInteger();

Round to closest BigInteger (12.99 → 13)

Use BigDecimal.setScale and BigDecimal.toBigInteger:

BigDecimal bigDec = ...
BigInteger bigInt = bigDec.setScale(0, RoundingMode.HALF_UP)
                          .toBigInteger();

See Also

Converting the other way around: Convert from BigInteger to BigDecimal

Comments

Be the first to comment!