# convert a really big string (len 39) to bigdecimal

• 06-07-2008, 11:18 AM
coolsig
convert a really big string (len 39) to bigdecimal
Hi ,
How do I convert a large String (length 39 ) to BigDecimal ?

eg
String c = "848595950484765868574748349586738596879";

try {
BigDecimal num2 = BigDecimal.valueOf((Long.parseLong(c)));
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}

I tried this, it throws a nfe. Any ideas on how to get this string parsed to an BigDecimal.

Thanks
• 06-07-2008, 02:27 PM
Zosden
try biginteger
• 06-07-2008, 02:49 PM
sukatoa
The way you initialize BigDecimal is strange for me....

I guess you should initialize first an Instance of it before you use valueOf(something) method....

But it may be simplified....
According to your string sample, that value may not be applicable to long, see what's the maximum value can long datatype handles...

You may also try Double,

eg.

Code:

String c = "848595950484765868574748349586738596879";
try {
BigDecimal num2 = new BigDecimal(new Double(c));
System.out.println(num2);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}

• 06-07-2008, 07:10 PM
hardwired
Code:

import java.math.BigDecimal;

public class Test {
public static void main(String[] args) {
String c = "848595950484765868574748349586738596879";
BigDecimal num2 = null;
try {
num2 = new BigDecimal(c);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
System.out.println("num2 = " + num2);
}
}

• 06-07-2008, 07:33 PM
coolsig
Thanks,
It worked :)
• 06-08-2008, 07:18 AM
sukatoa
You may now mark this thread as SOLVED....
• 06-14-2008, 02:48 AM
danielstoner