Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

custom currency formatting

 
fred flintstone
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
result:124.523.423.434,50

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?

Thanks,
Eric
 
Tony Docherty
Bartender
Posts: 2941
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider printf or String#format and the % tags as an alternative.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic