# How can I do this?

• March 7th, 2012, 08:59 AM
arvindbis
How can I do this?
my objective is to multiply any 2 numbers(int). The numbers can be extremely large (i.e. run into hundreds of digits) and are provided as strings. The expected output is a string which represents the product of the two numbers. "output shuld be a integer." here is what i coded.
Code :

```public class LargeMultiply { static String test1a = "268435456"; static String test1b = "524288";   static String testcaseA = test1a; static String testcaseB = test1b;   public static void main(String args[]){ LargeMultiply inst = new LargeMultiply(); String v = inst.multiply(testcaseA,testcaseB); System.out.println(v); }   public String multiply(String num1, String num2){ Long a=Long.parseLong(num1); Long b=Long.parseLong(num2); return String.valueOf(a*b); } }```

and here are outputs

if inputs are 268435456 524288 these then output is 140737488355328 the above code perform well in this case.
if inputs are 26843545623423 52428824234 then expected output is 1407375535307804420432982 this but it gives a -ve value due to max limit reached for "long" but if i use "double' then answer is in scientfic form and that is not needed i want output in integer form.

Is any way to display double in normal form(not with decimal point)??
• March 7th, 2012, 09:07 AM
KevinWorkman
Re: How can I do this?
You could probably use BigInteger, but I would guess you're supposed to write code that computes the answer manually. How do you multiply large numbers without a computer?
• March 7th, 2012, 10:01 AM
arvindbis
Re: How can I do this?
you mean to say that implement a algo. which actually we use in real life?
• March 7th, 2012, 10:06 AM
KevinWorkman
Re: How can I do this?
Quote:

Originally Posted by arvindbis
you mean to say that implement a algo. which actually we use in real life?

I'm only guessing because I don't have your requirements in front of me, but this is a pretty common homework assignment. You won't be able to multiply arbitrary-sized numbers by using a standard primitive, so you either have to implement the multiplication yourself or use BigInteger, which is probably cheating.
• March 7th, 2012, 10:23 AM
arvindbis
Re: How can I do this?
:-s the only requirement is to display result in integers, double does this but it has decimal piont and exponent system. thats it. I will try with biginteger

biginterger is damn good but still will use it untill i code something using primitive types.
• March 7th, 2012, 12:13 PM
KevinWorkman
Re: How can I do this?
Doubles might seem to work, but if there really is no limit on how many characters your digits can have, double won't cover all cases.

If you don't believe me, check out Double.MAX_VALUE. What happens if you use that as an operand?
• March 7th, 2012, 09:26 PM
arvindbis
Re: How can I do this?
Indeed, double.maximum is equals to 1.7976931348623157E308 very large number but here is the problem as yi=ou can see the decimal piont and "E308" the exponent part. All I want is how can I change position of decimal(dot) i.e. after 13 digits on left or something higher?
• March 7th, 2012, 09:54 PM
KevinWorkman
Re: How can I do this?
You can use DecimalFormat, but like I keep saying, that's not going to work for arbitrarily large numbers. If I was an instructor grading this program, I promise I would use Double.MAX_VALUE as a test case, which will break if you're doing it the way you're suggesting.