Announcement Announcement Module
No announcement yet.
CurrencyFormatter setting Currency myself? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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:

    @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

    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!