Actually, I am inputting a number 32 digits
long(e.g.,10001001100210031004100510061007)
as a String
and I have to convert into a datatype to perform an arithmetic operation. I am using java and I can't use BigInteger
class.
Asked
Active
Viewed 803 times
-3

Abhinav Kushagra
- 176
- 1
- 2
- 15
-
Why you cannot use BigInteger? What arithmetic operation do you need? (+, -, *, /, ???) – kennytm Feb 19 '17 at 14:30
-
@kennytm I'm working on an online site and it doesn't support it. – Abhinav Kushagra Feb 19 '17 at 14:32
-
2@AbhinavKushagra I'm pretty sure that Java implementations *have to* support BigInteger. Are you sure it's not simply that you aren't allowed to import it, and so have to use `java.math.BigInteger` explicitly instead? – Andy Turner Feb 19 '17 at 14:34
-
@CKing It's not a duplicate as I have stated there that I can't use 'BigInteger' class. – Abhinav Kushagra Feb 19 '17 at 14:34
-
@AbhinavKushagra Why can't you use `BigInteger` class? Just for fun? Even then, did you search the site for an answer before posting the question? How did the existing questions not answer your question? – Chetan Kinger Feb 19 '17 at 14:36
-
@CKing see above: the platform s/he uses does not support it – Izruo Feb 19 '17 at 14:38
-
@Izruo Fair enough but we do have answers to that question already I suppose on the site? See [this](http://stackoverflow.com/questions/5318068/how-to-handle-very-large-numbers-in-java-without-using-java-math-biginteger) for example? It would have been faster to search the site than to type that question IMO. – Chetan Kinger Feb 19 '17 at 14:40
-
I tried to use BigInteger class but it is giving me error on that site. @CKing – Abhinav Kushagra Feb 19 '17 at 14:42
-
1@AbhinavKushagra Did you search *Stack Overflow* before posting a question. See [this](http://stackoverflow.com/questions/5318068/how-to-handle-very-large-numbers-in-java-without-using-java-math-biginteger). – Chetan Kinger Feb 19 '17 at 14:42
-
@Cking They all have used BigIntger somewhere either directly or indirectly. – Abhinav Kushagra Feb 19 '17 at 14:45
-
@AbhinavKushagra the only reason to be using an online site which won't let you use `BigInteger` is because you're supposed to be completing an exercise to implement a basic version of the class yourself; in which case, asking here defeats the point of the exercise. If not, the online site is simply not worth wasting your time on. – Andy Turner Feb 19 '17 at 14:45
-
@AbhinavKushagra Did you even go through the link I shared above in my previous comment?? – Chetan Kinger Feb 19 '17 at 14:46
-
@CKing Yes, I have checked that link. – Abhinav Kushagra Feb 19 '17 at 14:48
-
@AbhinavKushagra And why is that not helpful? Do you want someone to copy paste the answer here? – Chetan Kinger Feb 19 '17 at 14:48
-
@AbhinavKushagra Which online site you are working on? – kennytm Feb 19 '17 at 14:49
-
@CKing Do you want me to create that class on that online site and then create methods to perform arithmetic operations? – Abhinav Kushagra Feb 19 '17 at 14:52
-
@AbhinavKushagra How else do you propose to solve this problem if you can't use `BigInteger`? If you don't want to create a class, copy paste what it does in a single method. I don't see any other option. – Chetan Kinger Feb 19 '17 at 14:54
-
@CKing neither do I. :( – Abhinav Kushagra Feb 19 '17 at 14:59
2 Answers
3
You can create a BigInteger
from your String
like this :
String s = "10001001100210031004100510061007";
BigInteger l = new BigInteger(s);
System.out.println(l);
EDIT
Like @Andy Turner said in comment you can use BigDecimal
like this :
String s = "10001001100210031004100510061007";
BigDecimal l = new BigDecimal(s);
System.out.println(l);

Youcef LAIDANI
- 55,661
- 15
- 90
- 140
-
2OP statet s/he can't use `BigInteger`. Edit: It seems to be an edit after your answer. – Izruo Feb 19 '17 at 14:26
-
-
-
but he said in the first "I am restricted to use BigInteger class" – Youcef LAIDANI Feb 19 '17 at 14:28
-
-
-
1@Izruo you can check the first question http://stackoverflow.com/revisions/42328331/1 he said "I am restricted to use BigInteger class" – Youcef LAIDANI Feb 19 '17 at 14:30
-
@YCF_L As I immediately stated in the edit, I saw the edit date of the question is longer in the past than the date of your answer, so I already guessed something like that. – Izruo Feb 19 '17 at 14:33
0
If you really cannot use the BigInteger
class, you will have to create a custom big number class, perhaps using a List<Integer>
or int[]
for underlying storage.
There are examples of this sort of thing on SO already, such as here, here, and here.

Community
- 1
- 1

hughjdavey
- 1,122
- 2
- 9
- 17
-
-
The answers I linked to provide implementations of `add` and `multiply`, and I guess the others would be done in a similar way. – hughjdavey Feb 19 '17 at 14:49
-
2@hughjdavey I believe the OP wants you to build the entire library for them. – Chetan Kinger Feb 19 '17 at 14:51