Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Math.floorDiv(a, b), but for BigInteger?

Tags:

java

Some way to make a Math.floorDiv(a, b); and return the floor division of two BigInteger values
An example:

BigInteger big0 = new BigInteger("10");
BigInteger big1 = new BigInteger("20");
Math.floorDiv(big0, big1);
like image 538
H4YZ_ Avatar asked Dec 14 '22 11:12

H4YZ_


1 Answers

Switch to BigDecimal and you can control rounding. A scale of 0 means the results are rounded to an integer and FLOOR rounds towards negative infinity.

big0.divide(big1, 0, RoundingMode.FLOOR)

(If you construct big0 from an int, long, or BigInteger then its scale will already be 0 and you can omit the middle argument.)

like image 176
John Kugelman Avatar answered Dec 31 '22 20:12

John Kugelman