4 Replies Latest reply on Feb 15, 2008 10:49 PM by 807603

# problem with bigdecimal conversion

Hi,

I want to convert the double number 25000000 to BigDecimal, but the result that I get is 2.5E+7 and this number causes that the application throws an exception.

I'm doing it this way:

double number=25000000;
String value = Double.toString(number);
BigDecimal bd = new BigDecimal(value);

The funny thing is that if I try with 25000000.12 it works fine... I tried to get the long value of the BigDecimal and that works for the 25000000 but no for 25000000.12, because I get no decimals...

Any idea of how can I get this to work?

Thanks in advance
• ###### 1. Re: problem with bigdecimal conversion
When you use the constructor of BigDecimal that takes a string, the precision of that string becomes the precision of the BigDecimal. In your example, it's precise to exactly no decimal places, hence so is your BigDecimal. Try this
``BigDecimal bd = new BigDecimal("25000000.12");``
Bingo. Your BigDecimal is, and always will be, precise to two decimal places
• ###### 2. Re: problem with bigdecimal conversion
Yes, that works, but I have to convert my double number to a String first, and that conversion gives as result 2.5E7, that's why I get 2.5E+7 as the result of the conversion to BigDecimal, but I can�t use the constructor exactly as you said...

Thanks for your reply
• ###### 3. Re: problem with bigdecimal conversion
Then do a different conversion that formats the number correctly. Are you using a DecimalFormat object to do the conversion?

Edit: No, I see you aren't. Use one.
• ###### 4. Re: problem with bigdecimal conversion
Yes, that worked very good.

Thank you very much!