Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you've answered your own question there... What are the differences between the two pieces of code?



 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the comma, thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lukasz Bajzel
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic