This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

I18N Currency Formatting Issue  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble applying a currency format to a numeric value and could use some thoughts/ideas/help.

For our Swiss German locale, we want to display price values with a currency symbol of "SFr.". I figured I could using the CurrencyInstance of the DecimalFormatter for the job. Here's the details:

The format is: SFr.#,##0.00

The code snippet is:

DecimalFormat df = (DecimalFormat)NumberFormat.getCurrencyInstance(Locale.getDefault());
// df.applyLocalizedPattern(strFormat);
df.applyPattern(strFormat);

No difference between .applyPattern() and .applyLocalaizedPattern(), both give the following exception:

java.lang.IllegalArgumentException: Multiple decimal separators in
pattern "SFr.#,##0.00"
at java.text.DecimalFormat.applyPattern(Unknown Source)
at java.text.DecimalFormat.applyPattern(Unknown Source)

Anyone have any ideas how I can apply this valid currency format? Note that I really can't extract the "SFr." every time. The format string could be passed in by the user and could be embedded, and plus it seems that currency symbol processing for the locale should be taken care of by the Java Locale processing.

Note that I start my VM with the following:
-Duser.language=de -Duser.region=CH

Thanks!
 
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


From the javadocs:

Special Pattern Characters

Many characters in a pattern are taken literally; they are matched during parsing and output unchanged during formatting. Special characters, on the other hand, stand for other characters, strings, or classes of characters. They must be quoted, unless noted otherwise, if they are to appear in the prefix or suffix as literals.
...
' Prefix or suffix No Used to quote special characters in a prefix or suffix, for example, "'#'#" formats 123 to "#123". To create a single quote itself, use two in a row: "# o''clock".

 
John Piekos
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the pointer, it helped - I read the javadoc but didn't "get it" the first time.

My issue is further compounded by the fact that the 3rd party spreadsheet control barfs on the escaped pattern (they must be doing their own escaping - I unescaped prior to setting, ugh) and more importantly, by the fact the user could enter any type of currency symbol in various positions in the user-specified pattern. Other problematic symbols could include: Antigua's "E.C. $", Bermuda's "Bd.$", Ecuador's "S/.
", Kenya's "Kshs.", Macau's "Ptc.", Panama's "B/.", Tanzania's "T.Shs." and a few (known) others... Too bad the Java (locale) currency processing doesn't take handle this automatically.

Anyway, thanks for the pointer, I got around my immediate issue and if I have to travel to Bermuda to fix any currency formatting issues they might have, well, then I guess I'll deal with that then. :-)
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!