• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Cannot parse a currency string value to a number using NumberFormat  RSS feed

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading the book OCP_ Oracle Certified Professional Java SE 8 Programmer II, and in a chapter talking about NumberFormat. the authors give the following example :

which, normaly will output : 92807.99,however, when I run it in my IDE here is what I get :

Exception in thread "main" java.text.ParseException: Unparseable number: "$92,807.99"
at java.text.NumberFormat.parse(NumberFormat.java:385)
at Test.main(Test.java:15)

 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The getCurrencyInstance() returns the currency format for the default locale. Do you use dollars in your current locale?

Henry
 
Saloon Keeper
Posts: 2153
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The grouping and decimal separators matter as well.  Ecuador and Canada-French use the dollar symbol, but comma as the decimal separator and period as the grouping separator.  Probably other locales do as well.
 
yas sine
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right, the default locale was fr_FR, therefore it doesn't work, but when I change it to en_US it works as charm
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!