my dog learned polymorphism*
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 Android Security Essentials Live Lessons this week in the Android 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: 8843
    
    7

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?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[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: 19656
    
  18

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
 
Consider Paul's rocket mass heater.
 
subject: Why does converting ####.### to double give an error?
 
Similar Threads
Type mismatch:Cannot convert from double to int.
Double Validation problem in Struts
How to retain decimal precisions after converting from String to double
need help! (one error)
String conversion in to double