Search code examples
javaalgorithmbigdecimal

Java how to operate on BigDecimal values


Given the following conditions:

  • two BigDecimal values: b1 and b2;
  • x - Integer;
  • b1 > b2;

I need to find

the minimum value of x such that

b2 * x > b1

For example, if all values - Integer, than:

 if ( b1%b2 == 0 ) {
   x = b1/b2;
 } else {
   x = b1/b2 +1; 
 }

Solution

  • You could use

    BigInteger x = b1.divide(b2, 0, RoundingMode.CEILING).toBigInteger()