public class PercentConverter extends DoubleConverter
ObjectConverter implementation for a percentage| Modifier and Type | Field and Description |
|---|---|
static ConverterContext |
CONTEXT |
CONTEXT_FIXED_1_DIGIT_FRACTION, CONTEXT_FIXED_2_DIGIT_FRACTION, CONTEXT_FIXED_4_DIGIT_FRACTION, PROPERTY_NUMBER_FORMAT| Constructor and Description |
|---|
PercentConverter() |
PercentConverter(java.util.Locale locale) |
PercentConverter(java.text.NumberFormat format) |
| Modifier and Type | Method and Description |
|---|---|
java.lang.Double |
fromString(java.lang.String string,
ConverterContext context)
Converts the String to a Double.
|
getNumberFormat, numberFromString, toStringfromString, getObjectConverterManager, toString, toStringConverterpublic static final ConverterContext CONTEXT
public PercentConverter()
public PercentConverter(java.util.Locale locale)
public PercentConverter(java.text.NumberFormat format)
public java.lang.Double fromString(java.lang.String string,
ConverterContext context)
AbstractNumberConverter.PROPERTY_NUMBER_FORMAT if any.
If not there, it will use AbstractNumberConverter.getNumberFormat() to get the NumberFormat to do the conversion.
Different from a DoubleConverter, if the String doesn't have a percentage sign at the end, we will divide the number from DoubleConverter by 100 and return the value. That's because "50%" actually is 0.5. If user forgot to include a % sign, for example, pass in "50", we should still treat it as percentage and return 0.5 as they are using a PercentConverter.
Please note, the conversion may involve rounding or truncation.
fromString in interface ObjectConverter<java.lang.Double>fromString in class DoubleConverterstring - the string to be converted.context - the context