-
Jun 3rd, 2012, 03:01 PM
#1
CurrencyFormatter setting Currency myself?
Hi there,
Is there a way to programatically set the CurrencyFormatter's currency?
I have a 'price' property on a Form and annotate it as follows:
@NotNull
@Min(0)
@NumberFormat(style = NumberFormat.Style.CURRENCY)
private BigDecimal price = null;
When CurrencyFormatter formats the price, it does that by using the request's Locale. What I would like to do is also format the price based on the request's Locale, but setting the currency based on some rules.
For example, a user with en_US locale might set the price to 5 EUR by putting '5' into the price input field and selecting 'EUR' from a dropdown. Out of the box, Spring formats the '5' as $ 5.00.
According to the documentation (and the source code), CurrencyFormatter has a (public) setCurrency method, but I'm not sure how to use it. See http://static.springsource.org/spring/docs/3.1.1.RELEASE/javadoc-api/org/springframework/format/number/CurrencyFormatter.html#setCurrency(java.util.Curre ncy).
Any idea how I can achieve what I explained above? I tried to create my own Formatter, but still can't pass in calculated variables (only static). This won't work:
@MyCurrencyFormat(currencyCode = getCurrencyCode())
private BigDecimal price = null;
although this would it does not achieve what I need:
@MyCurrencyFormat(currencyCode = "EUR")
private BigDecimal price = null;
Appreciate any ideas!
Nes
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules