Initialising BigInteger using double value is not a good way to go about it because double is not a precise representation of the actual number like 44.34. Below is the description of this problem and an appropriate way to avoid it.

If we initialise a BigInteger as follows:-

BigInteger myBigInt = new BigInteger(44.34);

There will be some precision loss i.e. it’s value will be either slightly greater then 44.34 or slightly lesser.

Hence the correct way of initialising BigInteger is as follows:-

BigInteger myPreciseBigInt = new BigInteger(“44.34”);

Passing the expected value as a String ensures absolute precision as we expected.

By spstech

2 thought on “Precision in initialising BigIntegers in Java”
  1. Hey there would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot quicker then most. Can you recommend a good web hosting provider at a reasonable price? Cheers, I appreciate it!

Leave a Reply

Your email address will not be published. Required fields are marked *