public class PercentageTypeConverter extends NumberTypeConverterSupport implements TypeConverter<java.lang.Number>
A locale aware number converter that parses percentages. Consistent with other areas of Java (and computing) values are divided by 100 before being returned. For example "100%" will return 1, "58%" will return 0.58 etc. The value returned is either a double, float, or BigDecimal depending on the target type supplied (which is usually driven by the type of the property being converted).
Modifier and Type | Field and Description |
---|---|
static java.util.regex.Pattern |
PRE_PROCESS_PATTERN
Pattern used to remove any spaces between the value and the % sign.
|
Constructor and Description |
---|
PercentageTypeConverter() |
Modifier and Type | Method and Description |
---|---|
java.lang.Number |
convert(java.lang.String input,
java.lang.Class<? extends java.lang.Number> targetType,
java.util.Collection<ValidationError> errors)
Converts the input to a subclass of Number based on the targetType provided.
|
protected java.text.NumberFormat[] |
getNumberFormats()
Returns a single percentage instance of NumberFormat.
|
protected java.lang.String |
preprocess(java.lang.String input)
Pre-processes the input by first using
NumberTypeConverterSupport.preprocess(String)
and further pre-processing by adding the % sign if it is missing, any removing any spaces
between the value and the % sign. |
getLocale, parse, setLocale
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
setLocale
public static final java.util.regex.Pattern PRE_PROCESS_PATTERN
protected java.text.NumberFormat[] getNumberFormats()
getNumberFormats
in class NumberTypeConverterSupport
protected java.lang.String preprocess(java.lang.String input)
NumberTypeConverterSupport.preprocess(String)
and further pre-processing by adding the % sign if it is missing, any removing any spaces
between the value and the % sign.preprocess
in class NumberTypeConverterSupport
input
- the String as input by the userpublic java.lang.Number convert(java.lang.String input, java.lang.Class<? extends java.lang.Number> targetType, java.util.Collection<ValidationError> errors)
convert
in interface TypeConverter<java.lang.Number>
input
- the String being convertedtargetType
- the Class representing the type of the property to which the return
value of the conversion will be assigned. In many cases this can be ignored as
converters will return a single type more often than not.errors
- an empty collection of validation errors that should be populated by the
converter for any errors that occur during validation that are user input related.? Copyright 2005-2006, Stripes Development Team.