0

So I am trying to parse a string containing a number too large for Int storage. But I need the value of the string because I am dividing it. Can I parse the string into a Long?

n = Long.parseLong(string);

like that? Or is there a way I can divid the string by a number without having to turn the string into a number? The code I am using is like this:

private static String getFinished(String x, int y){
    int word = Integer.parseInt(x);
    word = word/y;

And the number I am using is: 1980715126555015951540148577071512651650 But I could potentially be using numbers even longer.

  • 3
    If the number is less than or equal to 2^63 - 1 then you can use a `long`. If it's larger than that, use a `BigInteger`. More importantly, try it yourself and let us know if you get stuck with a real example. – Elliott Frisch Apr 17 '16 at 01:58
  • Upvoting... because it took me long enough to find the solution (already knowing it even) that I can see why you needed to post the question. – djechlin Apr 17 '16 at 02:02

3 Answers3

3

Can I parse the string into a Long?

Yes. Provided that the number isn't too big. The range for a long / Long is -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

Another alternative is to use BigInteger (javadoc). It can cope with numbers that are even larger. (The upper bound for BigInteger is most likely determined by the amount of memory you have available.)

The number I am using is: 1980715126555015951540148577071512651650

That requires a BigInteger. Example:

BigInteger small = new BigInteger("1980715126555015951540148577071512651650");
BigInteger notQuiteSoSmall = small.add(BigInteger.ONE);

Notes:

  1. There is no operator overloading for BigInteger. You have to use explicit method calls.
  2. BigInteger objects are like String objects; i.e. they are immutable. Arithmetic methods produce a new BigInteger object.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Can I use BigInteger the same way I would use the Int data type? – The TwistyTie Apr 17 '16 at 02:02
  • @TheTwistyTie No. `int` and `long` are primitives and can be used directly. `BigInteger` is an *immutable* object, so you must call methods to perform calculations. E.g. for an `int` you can say `i = i + 5`, but for a `BigInteger` you have to say `bi = bi.add(BigInteger.valueOf(5))` – Andreas Apr 17 '16 at 02:03
  • No. You have to use method calls to do arithmetic. There are no overloads for the arithmetic operators. – Stephen C Apr 17 '16 at 02:04
  • So how would that look interms of parsing a string into a BigInteger? – The TwistyTie Apr 17 '16 at 02:09
  • @TheTwistyTie Look at the javadoc. StephenC has provided the link for you in the answer. There is a constructor you can use. – Andreas Apr 17 '16 at 02:12
1

The class you are looking for is BigInteger. Or BigDecimal for floats.

djechlin
  • 59,258
  • 35
  • 162
  • 290
0

No you can not divide a string without parsing it into a number.

And long num = Long.parseLong(str); is 100% legal, provided you wrapping it with a try/catch block or throwing exception from method.

Edit: Based upon example string you added, long might not help you anymore. Check BigInteger.

Raman Shrivastava
  • 2,923
  • 15
  • 26