ANteelauh Farnese ANteelauh Farnese - 8 months ago 27
Java Question

How to divide a BigInteger by a plain ol' integer?

I've been learning some Java and I need help with the following:

I want to divide a BigInteger by a regular integer (i.e. int) but I don't know how to do this. I did a quick search on Google and on Stack Exchange but didn't find any answers.

So, how can I divide a BigInteger by an int? And while we're at it, how can I add/subtract BigInts to ints, compare BigInts to ints, et cetera? Thanks in advance for any helpful help I get.


Convert the Integer to BigInteger and than divide both BigInteger, as following:

BigInteger b = BigInteger.valueOf(10);
int x = 6;

//convert the integer to BigInteger.

BigInteger converted = new BigInteger(Integer.toString(x));
//now you can divide, add, subtract etc.

BigInteger result = b.divide(converted);  //but this will give you Integer values.


result = b.add(converted);


Above division will give you Integer values of divisions, to get exact values, use BigDecimal.