Search code examples
javac++abstract-data-type

Data type in c++ or java for holding 20 digit integer


Is there any data type available in Java or C++ which can hold integer values of 20 digits or more? The long long data type can hold only till 18 digits.


Solution

  • Java specific:

    You are looking for BigInteger

    Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types)

    For ex:

      BigInteger bint = new BigInteger("1234567856656569");
      BigInteger bint2 = new BigInteger("1234556567856656569");
      System.out.println(bint2.intValue()-bint.intValue()); //397189120
    

    And BigDecimal