Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calculation of large digit-plz help

 
Ka Tanaka
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone know how to calculate a large number(longer # of digit than long type -> can go up to as long as possible)in java?
the numbers are inputed as string, adding subtracting, multi and div operation are require..thz alot
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if it is longer than long type, what you can do is to define your own function:
1) treat them as string, read in one character at a time.
2) use character comparision to get the number value, then use the normal number calculation.
3) use a loop to calculate every digit from right to left.
4) then depend on what you want as result. If you want string, then you can transform the result back to string again. Or you can use formula below to represent the number value.
N = d_0 * 10^0 + d_1 * 10^1 + ... + d_n * 10^n
HTH.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you may have to use this:
N = d_0 + 10 * ( d_1 + 10 * (d_2 + 10 * (d_3 + ( ... ))))
to represent the number value.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BigInteger
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic