org.apache.commons.validator.routines
Class PercentValidator
- Serializable
public class PercentValidator
Percentage Validation and Conversion routines (
java.math.BigDecimal
).
This is one implementation of a percent validator that has the following features:
- It is lenient about the the presence of the percent symbol
- It converts the percent to a
java.math.BigDecimal
However any of the
number validators can be used for
percent validation.
For example, if you wanted a
percent validator that converts to a
java.lang.Float
then you can simply instantiate an
FloatValidator
with the appropriate
format type:
... = new FloatValidator(false, FloatValidator.PERCENT_FORMAT);
Pick the appropriate validator, depending on the type (i.e Float, Double or BigDecimal)
you want the percent converted to. Please note, it makes no sense to use
one of the validators that doesn't handle fractions (i.e. byte, short, integer, long
and BigInteger) since percentages are converted to fractions (i.e
50%
is
converted to
0.5
).
$Revision: 386637 $ $Date: 2006-03-17 13:22:26 +0000 (Fri, 17 Mar 2006) $
static BigDecimalValidator | getInstance() - Return a singleton instance of this validator.
|
protected Object | parse(String value, Format formatter) - Parse the value with the specified
Format .
|
determineScale , getFormat , getFormat , getFormatType , isAllowFractions , isInRange , isValid , maxValue , minValue , parse , processParsedValue |
format , format , format , format , format , getFormat , isStrict , isValid , isValid , isValid , isValid , parse , processParsedValue |
PERCENT_SYMBOL
private static final char PERCENT_SYMBOL
DecimalFormat's percent (thousand multiplier) symbol
POINT_ZERO_ONE
private static final BigDecimal POINT_ZERO_ONE
PercentValidator
public PercentValidator()
Construct a strict instance.
PercentValidator
public PercentValidator(boolean strict)
Construct an instance with the specified strict setting.
strict
- true
if strict
Format
parsing should be used.
parse
protected Object parse(String value,
Format formatter)
Parse the value with the specified
Format
.
This implementation is lenient whether the currency symbol
is present or not. The default
NumberFormat
behaviour is for the parsing to "fail" if the currency
symbol is missing. This method re-parses with a format
without the currency symbol if it fails initially.
- parse in interface AbstractFormatValidator
value
- The value to be parsed.formatter
- The Format to parse the value with.
- The parsed value if valid or
null
if invalid.
Copyright (c) 2001-2004 Apache Software Foundation