Forums Register Login

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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
I think you've answered your own question there... What are the differences between the two pieces of code?



+Pie Number of slices to send: Send
It's the comma, thanks.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
Get off me! Here, read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1137 times.
Similar Threads
How to retain decimal precisions after converting from String to double
need help! (one error)
Type mismatch:Cannot convert from double to int.
Double Validation problem in Struts
String conversion in to double
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:13:48.