I’m having some problems in the moment of parsing String to Doubles, with Spring and Java. This is not totally related to Spring, but it may put you in situation.
I had a CustomNumberEditor, for parsing easily Strings to Doubles.
java.text.DecimalFormat was used as the decimal format for parsing double to string. However, when you do numberFormat.parse(stringDouble) if the string starts with numbers, and follow with letters, the value returned is the numbers. i.e. 12a is parsed to 12. For me this is clearly and error, and I would like to solve this easily.
I imagine this should be implemented in some other kind of numberFormat, or attributes or something, but I could not find it. Any ideas?
The
parse(...)method won’t consume all the text and thus would allow “numbers” like12aetc. (this might also be a currency like12USD).Your
CustomNumberEditormight, however, just apply a number only pattern toString#matches(...)in order to check the string only contains a number before parsing it – something like^-?\d+\.?\d*$(String must only contain digits, a minus sign and a separator) . You should note however, that this is locale dependent and not as flexible as the decimal format pattern.