Search code examples
javabigintegernotation

How to convert a BigInteger to a scientific notation string and back?


I want to convert BigInteger number into a small scientific notation like 1.86e+6 and again reconvert that scientific notation into BigInteger number in Java. Please help.


Solution

  • The easiest way is to use a BigDecimal to parse or output the scientific notation string.

    You can can do something like:

    BigDecimal bd = new BigDecimal("1.86E+6");
    BigInteger bi = bd.toBigInteger();
    

    and reverse:

    bd = new BigDecimal(bi);
    String s = bd.toString();
    

    Update

    If you need more user-defined output, then you can use NumberFormatter. Don't forget to set the Locale to something like Locale.ROOT, so you won't get, say a comma as the decimal separator (which is what I got first, in the German locale). Example:

        // Create a large, ugly number.
        BigInteger bi = BigInteger.valueOf(1001).pow(345);
        
        // Convert to scientific notation using invariant Locale.ROOT
        NumberFormat formatter = new DecimalFormat("0.######E0", DecimalFormatSymbols.getInstance(Locale.ROOT));
        String str = formatter.format(bi);
        
        System.out.println(bi);
        System.out.println();
        System.out.println(str);
        
        // No need for a formatter here.
        BigDecimal bd = new BigDecimal(str);
        BigInteger output = bd.toBigInteger();
        
        System.out.println();
        System.out.println(output);
    

    The output of this is:

    1411746534642247386926682895653605505594156007645319584856454985824576909030651172402258145880680847831210464829918490010166811079002972726937057623073994129575640372154237767652434547101885830188958723868572869883365738143353635151476880747344348010706072986535185748982964423793694140085891791220972791882178323235649877119554541663599295787824745711388310165587991341807160511741076029768404282877856115942906536866189181255514197337418597936644390730217525723115231014147849887446040444969336884906158293521291748134217314005889949484320602720371789914893639795254884800520873191697159041280591046403928290350948505388703036712226506136642305960716764124836947362932720418554290195995002114233675196543233402547357577387336805972842986766416381431727078044233139876612983206051371851773391882427929601311695575660371227105236375213782469513349953017524299926322617324052803634576283153878896093739315873095260971811967828941219651149370566639839402498088185721432957408746669159107050035686712174548658001777149571278954599340345001
    
    1.411747E1035
    
    1411747000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    

    (Yes, BigIntegers can be unwieldy).

    I don't know how big your values are, but the above works well on my Mac. Of course, using scientific notation with only a few decimal digits will lose a lot of precision.