| 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: 8262
|
|
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: 19216
|
|
|
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
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
|
 |
 |
|
|
subject: Why does converting ####.### to double give an error?
|
|
|