I’m working on a Spring MVC Project and ran into a problem with the internationalization in forms, especially the number formatting.
I already use fmt:formatNumber to format the numbers according to the current selected locale.
<fmt:formatNumber value="${object[field]}"/>
Like this, number formatting works well when I display numbers. But how about the forms?
At the moment, the input fields that are supposed to receive float values are prefilled with 0.0 and expect me to use “.” as decimal separator, no matter what locale is selected. Values containing “,” are refused by the server (…can not convert String to required type float…).
How can I make my input fields use and accept the appropriate number format as well?
Did you have a look at
@NumberFormat? If you annotate the property the input field is bound to, this should result in the proper formatting. Something like:This style is the “general-purpose number format for the current locale”. I guess, the current locale is determined threadwise from the
LocaleContextHolder.Your app needs to be annotation-driven, also see the section “Annotation-driven Formatting” in the docs.
You might want to take a look at the
DecimalFormatSymbolsas suggested in this answer.