Class FormattedStringToNumber
java.lang.Object
org.springframework.binding.convert.converters.StringToObject
org.springframework.binding.convert.converters.FormattedStringToNumber
- All Implemented Interfaces:
Converter
,TwoWayConverter
A converter for common number types such as integers and big decimals. Allows the configuration of an explicit number
pattern and locale.
Works with a general purpose
DecimalFormat
instance returned by calling
NumberFormat.getInstance(Locale)
by default. This instance supports parsing any number type generally and
will not perform special type-specific logic such as rounding or truncation. Subclasses may override.
Will coerse parsed Numbers to the desired numberClass as necessary. If type-coersion results in an overflow
condition; for example, what can occur with a Long being coersed to a Short, an exception will be thrown.- Author:
- Keith Donald
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected Number
convertToNumberClass
(Number number, Class<? extends Number> numberClass) Coerces the Number object returned by NumberFormat to the desired numberClass.boolean
If this Converter is "lenient" in parsing number strings.void
setLenient
(boolean lenient) Sets if this Converter should parse leniently.void
setNumberFormatFactory
(NumberFormatFactory numberFormatFactory) Sets the factory that returns theNumberFormat
instance that will format numbers handled by this converter.protected Object
protected String
Methods inherited from class org.springframework.binding.convert.converters.StringToObject
convertSourceToTargetClass, convertTargetToSourceClass, getSourceClass, getTargetClass
-
Constructor Details
-
FormattedStringToNumber
public FormattedStringToNumber() -
FormattedStringToNumber
-
-
Method Details
-
setNumberFormatFactory
Sets the factory that returns theNumberFormat
instance that will format numbers handled by this converter.- Parameters:
numberFormatFactory
- the number format factory
-
getLenient
public boolean getLenient()If this Converter is "lenient" in parsing number strings. A lenient converter does not require that all characters in the String be parsed successfully. Default is false.- Returns:
- the lenient flag
-
setLenient
public void setLenient(boolean lenient) Sets if this Converter should parse leniently.- Parameters:
lenient
- the lenient flag
-
toObject
- Specified by:
toObject
in classStringToObject
-
toString
- Specified by:
toString
in classStringToObject
-
convertToNumberClass
protected Number convertToNumberClass(Number number, Class<? extends Number> numberClass) throws IllegalArgumentException Coerces the Number object returned by NumberFormat to the desired numberClass. Subclasses may override.- Parameters:
number
- the parsed number- Returns:
- the coersed number
- Throws:
IllegalArgumentException
- when an overflow condition occurs during coersion
-