I have a need to work with large numbers (something in range 1E100 - 1E200). However, the BigInteger class, which seems to be suitable in general, does not recognize strings in scientific format during initialization, as well as does not support the conversion to a string in the format.
BigDecimal d = new BigDecimal("1E10"); //works
BigInteger i1 = new BigInteger("10000000000"); //works
BigInteger i2 = new BigInteger("1E10"); //throws NumberFormatException
System.out.println(d.toEngineeringString()); //works
System.out.println(i1.toEngineeringString()); //method is undefined
Is there a way around? I cannot imagine that such class was designed with assumption that users must type hundreds of zeros in input.
Scientific notation applies to
BigIntegers only in a limited scope - i.e. when the number in front ofEis has as many or fewer digits after the decimal point than the value of the exponent. In all other situations some information would be lost.Java provides a way to work around this by letting
BigDecimalparse the scientific notation for you, and then converting the value toBigIntegerusingtoBigIntegermethod:Conversion to scientific notation can be done by constructing
BigDecimalusing a constructor that takesBigInteger: