Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 23510
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: 1930
239
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
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!