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

1. Member
Join Date
Jun 2008
Posts
2
Rep Power
0

## 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

2. try biginteger

3. 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.

Java Code:
```String c = "848595950484765868574748349586738596879";
try {
BigDecimal num2 = new BigDecimal(new Double(c));
System.out.println(num2);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}```
Last edited by sukatoa; 06-07-2008 at 03:52 PM. Reason: Typo

4. Java 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);
}
}```

5. Member
Join Date
Jun 2008
Posts
2
Rep Power
0
Thanks,
It worked :)

6. You may now mark this thread as SOLVED....

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•