jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes custom currency formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "custom currency formatting" Watch "custom currency formatting" New topic

custom currency formatting

fred flintstone

Joined: Feb 18, 2009
Posts: 1
So I'm trying to do some custom currency formatting. There are some bugs/outdated data in the current java locale currency formatting results. I could update my locale database using http://userguide.icu-project.org/icu4j-locale-service-provider#TOC-Using-ICU4J-Locale-Service-Provider

but I just want to be able to format some currencies by hand using either NumberFormat or DecimalFormat.

Here is my test class:

This results in:

So if you notice, that for result, the currency symbol is missing. How come the DecimalFormatSymbols doesn't make the currency symbol appear when I set it?

Another question is, what if I wanted to make the currency symbol appear AFTER the number?

Tony Docherty

Joined: Aug 07, 2007
Posts: 2737
If you want the currency symbol to display you have a few choices, such as:

1. Use the NumberFormat.getCurrencyInstance() method to get a formatter that handles currencies.
2. Supply your own pattern which outputs the currency symbol.

Another question is, what if I wanted to make the currency symbol appear AFTER the number?

If you are using option 1 above, I would imagine the currency formatter will put the symbol in an appropriate location for that currency. If you want to change that behaviour then I believe you will have to supply your own pattern.
If you are using option 2 then you are supplying a pattern so you can put the symbol wherever you want

For details on setting your own pattern see the DecimalFormat classes API docs.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44538
Consider printf or String#format and the % tags as an alternative.
I agree. Here's the link: http://aspose.com/file-tools
subject: custom currency formatting
jQuery in Action, 3rd edition