public class PercentageTypeConverter extends NumberTypeConverterSupport implements TypeConverter<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 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 |
|---|---|
Number |
convert(String input,
Class<? extends Number> targetType,
Collection<ValidationError> errors)
Converts the input to a subclass of Number based on the targetType provided.
|
protected NumberFormat[] |
getNumberFormats()
Returns a single percentage instance of NumberFormat.
|
protected String |
preprocess(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, setLocaleclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitsetLocalepublic static final Pattern PRE_PROCESS_PATTERN
protected NumberFormat[] getNumberFormats()
getNumberFormats in class NumberTypeConverterSupportprotected String preprocess(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 NumberTypeConverterSupportinput - the String as input by the userpublic Number convert(String input, Class<? extends Number> targetType, Collection<ValidationError> errors)
convert in interface TypeConverter<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.