• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's the comma, thanks.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic