public abstract class NumberConverter extends AbstractConverter
Converter
implementaion that handles conversion
to and from java.lang.Number objects.
This implementation handles conversion for the following
java.lang.Number
types.
java.lang.Byte
java.lang.Short
java.lang.Integer
java.lang.Long
java.lang.Float
java.lang.Double
java.math.BigDecimal
java.math.BigInteger
setUseLocaleFormat(true)
setLocale(Locale)
setPattern(String)
setPattern(String)
setLocale(Locale)
toNumber(String)
method is used to convert
from String to Number and the Number's
toString()
method used to convert from
Number to String.
N.B.Patterns can only be specified using the standard
pattern characters and NOT in localized form (see java.text.DecimalFormat
).
For example to cater for number styles used in Germany such as 0.000,00
the pattern
is specified in the normal form 0,000.00
and the locale set to Locale.GERMANY
.
Constructor and Description |
---|
NumberConverter(boolean allowDecimals)
Construct a java.lang.Number Converter
that throws a
ConversionException if a error occurs. |
NumberConverter(boolean allowDecimals,
Object defaultValue)
Construct a
java.lang.Number Converter that returns
a default value if an error occurs. |
Modifier and Type | Method and Description |
---|---|
protected String |
convertToString(Object value)
Convert an input Number object into a String.
|
protected <T> T |
convertToType(Class<T> targetType,
Object value)
Convert the input object into a Number object of the
specified type.
|
Locale |
getLocale()
Return the Locale for the Converter
(or
null if none specified). |
String |
getPattern()
Return the number format pattern used to convert
Numbers to/from a
java.lang.String
(or null if none specified). |
boolean |
isAllowDecimals()
Return whether decimals are allowed in the number.
|
void |
setLocale(Locale locale)
Set the Locale for the Converter.
|
void |
setPattern(String pattern)
Set a number format pattern to use to convert
Numbers to/from a
java.lang.String . |
void |
setUseLocaleFormat(boolean useLocaleFormat)
Set whether a format should be used to convert
the Number.
|
String |
toString()
Provide a String representation of this number converter.
|
conversionException, convert, convertArray, getDefault, getDefaultType, handleError, handleMissing, isUseDefault, setDefaultValue
public NumberConverter(boolean allowDecimals)
ConversionException
if a error occurs.allowDecimals
- Indicates whether decimals are allowedpublic NumberConverter(boolean allowDecimals, Object defaultValue)
java.lang.Number
Converter that returns
a default value if an error occurs.allowDecimals
- Indicates whether decimals are alloweddefaultValue
- The default value to be returnedpublic boolean isAllowDecimals()
public void setUseLocaleFormat(boolean useLocaleFormat)
useLocaleFormat
- true
if a number format
should be used.public String getPattern()
java.lang.String
(or null
if none specified).
See java.text.DecimalFormat
for details
of how to specify the pattern.
public void setPattern(String pattern)
java.lang.String
.
See java.text.DecimalFormat
for details
of how to specify the pattern.
pattern
- The format pattern.public Locale getLocale()
null
if none specified).public void setLocale(Locale locale)
locale
- The locale to use for conversionprotected String convertToString(Object value) throws Throwable
convertToString
in class AbstractConverter
value
- The input value to be convertedThrowable
- if an error occurs converting to a Stringprotected <T> T convertToType(Class<T> targetType, Object value) throws Throwable
convertToType
in class AbstractConverter
T
- Target type of the conversion.targetType
- Data type to which this value should be converted.value
- The input value to be converted.Throwable
- if an error occurs converting to the specified typepublic String toString()
toString
in class AbstractConverter
Copyright © 2000–2016 The Apache Software Foundation. All rights reserved.