Benjamin
Benjamin

Reputation: 1183

Enter a currency value into Vaadin TextField with comma as decimal separator

I use a simple TextField to enter a currency value and i want to transfer it to a BigDecimal. Now I want to enter values like "145,53" (comma separated decimals, as an usual currency value for €). But it only accepts the value if I use "145.53".

How can i apply this in a correct way?

Upvotes: 0

Views: 3281

Answers (1)

Jose Luis Martin
Jose Luis Martin

Reputation: 10709

Set a custom converter for the TextField.

Start with something like

public class CurrencyConverter extends StringToNumberConverter {

    @Override
    protected NumberFormat getFormat(Locale locale) {
         if (locale == null) {
                locale = Locale.getDefault();
            }

            return NumberFormat.getCurrencyInstance(locale);
    }

}

If you are using Spring, use LocaleContextHolder.getLocale() instead Locale.getDefault()

Upvotes: 2

Related Questions