0

Possible Duplicate:
How to add two numbers of any length in java?

can anyone help me with this?

what i need to do is to add a very large number that a calculator doesn't hold.

this is the code i have though it can only hold a number until 2147483647.. any number higher than that i get the error message stating "integer number too large"

can anyone tell me how can i use a larger number?

import java.math.BigDecimal;

public class AddTwoBigNumbers{
  public static void main() {
  BigDecimal num1, num2;
  num1 = new BigDecimal(2147483647);
  num2 = new BigDecimal(2147483647);
  Sum(num1, num2);
  }

  public static void Sum(BigDecimal val1, BigDecimal val2){
  BigDecimal sum = val1.add(val2);
  System.out.println("Sum of two BigDecimal numbers: "+ sum);
  }
}
Community
  • 1
  • 1
  • Following links may help you... [A similar question][1] [Another Similar Question][2] [1]: http://stackoverflow.com/questions/3748846/how-to-add-two-numbers-of-any-length-in-java [2]: http://stackoverflow.com/questions/5318068/very-large-numbers-in-java-without-using-java-math-biginteger – Amit Dec 08 '11 at 06:20
  • [Check this][1] [1]: http://stackoverflow.com/questions/5318068/very-large-numbers-in-java-without-using-java-math-biginteger – Ashish Dec 08 '11 at 06:22
  • To add these two number together all you need is a `long`. Sometimes it is best to use the simplest solution for your problem. – Peter Lawrey Dec 08 '11 at 08:01

7 Answers7

5

Sounds like you want to use BigInteger


[edit] To pass a number larger than Integer.MAX_VALUE to the constructor, use a long:

new BigInteger(9876543210L)

If you need a number larger than a long can hold, you'll need to find some other way of constructing it (like passing it in as a string, or multiplying two BigIntegers).

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
1

When you construct your BigDecimal, you are doing it with an integer. In Java, integers can only hold 2^31, so when you construct your BigDecimal that way, that's all it will allow. The compiler will complain if you give it a larger int.

The way to solve this is to pass in a String representation of your number.

num1 = new BigDecimal("123123123123123123123123");
Todd
  • 30,472
  • 11
  • 81
  • 89
0

num1 = new BigDecimal(2147483648l); num2 = new BigDecimal(2147483649l);

BigDecimal constructor supports long/double as parameter, use one of these.

harsh
  • 7,502
  • 3
  • 31
  • 32
0

Numbers are are always interpreted as ints. if you want the number to be compiled as long, you should add the suffix "L" to it, so it will be 2147483647333L. In case for numbers bigger than MAX_LONG (~2^31) you should provide them as byte arrays using this constructor with the string representation of the number ("1234.56").

David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
0

Are you asking how to create the BigDecimal (or BigInteger) with a larger number? If so, first thing you should do is append an L to your number literal -- that makes it a long instead of an int. If that's not big enough, you can also create new BigDecimals/BigIntegers by passing them a String representation of the number.

yshavit
  • 42,327
  • 7
  • 87
  • 124
0

You should use the constructor that accepts the value as String otherwise you are limited by the size of the data type you are using, to avoid which you wanted to use BigXXXX classes.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
0

BigDecimal will take a long in the constructor. But you will need an L at the end.

new BigDecimal(2147483648L) should work.

user949300
  • 15,364
  • 7
  • 35
  • 66