Precision in initialising BigIntegers in Java

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.



2 comments