aspose file tools*
The moose likes Beginning Java and the fly likes Why does converting ####.### to double give an error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does converting ####.### to double give an error?" Watch "Why does converting ####.### to double give an error?" New topic
Author

Why does converting ####.### to double give an error?

Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
When I convert a number ####.###(1046.571) using

String x = "1046.571";
double double_val = Double.parseDouble(x);

I get an exception error "Fail to convert to internal representation".

I do not understand how there can be a conversion error with the string value being converted to a double.

Isn't the String value within the double range?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

The code as you have it above works fine for me.
The only references I can find in a search involve JDBC. Are you giving us the full story here?
Can you give us a short sample of code that exhibits this behavior?


[How To Ask Questions On JavaRanch]
Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
I should have been more precise here is the exact code:

String x = "1,046.57100000";
double double_val = Double.parseDouble(x);
[ January 15, 2008: Message edited by: Fritz Largosa ]
Mark Newton
Ranch Hand

Joined: Jan 31, 2006
Posts: 129
I think you've answered your own question there... What are the differences between the two pieces of code?



Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
It's the comma, thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

You can still use the format with the comma if you switch to a NumberFormat, most probably DecimalFormat. With this class, you can specify your own format.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Lukasz Bajzel
Greenhorn

Joined: Dec 03, 2007
Posts: 26
Working with numbers and currencies is sometimes tricky. As suggested above, you may want to start using java.text.NumberFormat and its subclasses. They help in formatting different numbers and currencies.

Hope this helps.

Sincerly
Your friends at www.javaadvice.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does converting ####.### to double give an error?