Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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

 
Ranch Hand
Posts: 36
  • 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
Sheriff
Posts: 23571
138
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: 2022
273
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: 36
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!