• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Greenhorn
Posts: 26
  • 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: 23384
128
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: 1723
235
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
Greenhorn
Posts: 26
  • 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
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!