20

Is there a really large variable type I can use in Java to store huge numbers (up to around forty digits)?

long's maximum value is 9223372036854775807, which is 19 digits -- not nearly large enough.

I'm trying to create a calculator that can handle large numbers, because most nowadays can only hold an insufficient 10 digits or so, and I want want accurate calculations with numbers of a much larger magnitude

EDIT

Thanks for the answers. I can use BigInteger for big integers, the only limit being the computer's memory (should be sufficient). For decimals, I'll use float ^e, as @WebDaldo suggested, or BigDecimal (similar to BigInteger), as @kocko suggested.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94

5 Answers5

34

You can use BigInteger class.

BigInteger bi1 = new BigInteger("637824629384623845238423545642384"); 
BigInteger bi2 = new BigInteger("3039768898793547264523745379249934"); 

BigInteger bigSum = bi1.add(bi2);
        
BigInteger bigProduct = bi1.multiply(bi2);
       
System.out.println("Sum : " + bigSum);
System.out.println("Product : " + bigProduct);

Output:

Sum : 3677593528178171109762168924892318

Product : 1938839471287900434078965247064711159607977007048190357000119602656

I should mention BigDecimal, which is excellent for amount calculations compare to double.

BigDecimal bd = new BigDecimal("123234545.4767");
BigDecimal displayVal = bd.setScale(2, RoundingMode.HALF_EVEN);

NumberFormat usdFormat = NumberFormat.getCurrencyInstance(Locale.US);        
System.out.println(usdFormat.format(displayVal.doubleValue()));

Output:

$123,234,545.48

Community
  • 1
  • 1
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
  • 1
    do you know what the largest value of `BigInteger` is? – Jonathan Lam Aug 26 '13 at 12:48
  • @Jon: Probably somewhere above 2^1000000. Depends on how much memory you have, really. – cHao Aug 26 '13 at 12:51
  • 3
    @Jon It's `(2^32)^(2^31-1)`. – arshajii Aug 26 '13 at 12:52
  • can i use; `BigInteger testData = Math.random() * LIMIT + 1;` ?? – roottraveller Apr 21 '17 at 04:33
  • @rootTraveller You must understand the differences among different types, primitives vs objects, etc. If you want to randomize, look at these answers: http://stackoverflow.com/questions/8244691/generating-very-large-random-numbers-java and http://stackoverflow.com/questions/2290057/how-to-generate-a-random-biginteger-value-in-java – Sajal Dutta Apr 21 '17 at 04:51
12

You can try using the BigInteger class for operations with really huge integer numbers.

For operations with floating numbers, Java provides the BigDecimal class, which can be useful, as well.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
4

For calculations with exponents, like you would use in a calculator, you should use BigDecimal. The problem with BigInteger is that it only handles integers (no fractional numbers) and that for really big numbers like 10^100 it stores all the zeros, using a lot of memory, instead of using a format based on scientific notation.

You could alternatively use the floating point number type double, which gives you a large range of values, low memory usage and fast operations. But because of rounding issues and limited precision (around 16 decimal digits), I wouldn't recommend using it unless you really know what you're doing.

cHao
  • 84,970
  • 20
  • 145
  • 172
Joni
  • 108,737
  • 14
  • 143
  • 193
2

You can use float ^e

so you could have

0.55342663552772737682136182736127836782163 * 10^e

Calculators are mostly use that, too.

JavaDM
  • 851
  • 1
  • 6
  • 29
  • 1
    A float has only about 8 decimal digits of precision. You probably should avoid using it. – Joni Aug 26 '13 at 12:55
  • 1
    (http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3) The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York). – JavaDM Aug 26 '13 at 12:58
  • 1
    You do know that 32 bits means that only 23 are used for the mantissa? And that with 23 bits you can only represent numbers with 7-8 digits, right? – Joni Aug 26 '13 at 13:28
1

This is for all bigger numbers above 15 since using int blows it. You may want to find the factorial of 50 or 100 0r 500.

// Recursive version of the Fat factorial for bigger numbers ex: Factorial of 500     
BigInteger fatFactorial(int b) {
    if (BigInteger.ONE.equals(BigInteger.valueOf(b))
        || BigInteger.ZERO.equals(BigInteger.valueOf(b))) {
            return BigInteger.ONE;
    } else {
        return BigInteger.valueOf(b).multiply(fatFactorial(b - 1));
        }
    }
  • 5
    Although the code is appreciated, it should always have an accompanying explanation. This doesn't have to be long but it is expected. – peterh May 17 '15 at 06:40
  • This is for all bigger numbers above 15 since using int blows it. You may want to find the factorial of 50 or 100 0r 500. This works. – kamals1986 May 17 '15 at 08:36
  • public class Factorial { BigInteger fatFactorial(int b) { if (BigInteger.ONE.equals(BigInteger.valueOf(b)) || BigInteger.ZERO.equals(BigInteger.valueOf(b))) { return BigInteger.ONE; } else { return BigInteger.valueOf(b).multiply(fatFactorial(b - 1)); } } public static void main(String[] args) { Factorial fac = new Factorial(); System.out.println("Fat Numbers Fact of " + 500 + " = " + fac.fatFactorial(500)); } } – kamals1986 May 17 '15 at 08:38
  • 2
    No, no! Edit it into your answer. The explanation is not for me, it is for the googlers of the future (and for the java beginners). – peterh May 17 '15 at 08:41
  • No, you don't need to include the whole class, it doesn't make things more clear. Your original code snippet is enough, but give an explanation with that. – peterh May 17 '15 at 08:42
  • added it in the comment. – kamals1986 May 17 '15 at 08:55