Class CurrencyValidator
- All Implemented Interfaces:
Serializable
Currency Validation and Conversion routines (java.math.BigDecimal).
This is one implementation of a currency validator that has the following features:
- It is lenient about the presence of the currency symbol
- It converts the currency to a
java.math.BigDecimal
However any of the number validators can be used for currency validation.
For example, if you wanted a currency validator that converts to a
java.lang.Integer then you can simply instantiate an
IntegerValidator with the appropriate format type:
... = new IntegerValidator(false, IntegerValidator.CURRENCY_FORMAT);
Pick the appropriate validator, depending on the type (for example, Float, Double, Integer, Long and so on) you want the currency converted to. One thing to note - only the CurrencyValidator implements lenient behavior regarding the currency symbol.
- Since:
- 1.3.0
- See Also:
-
Field Summary
Fields inherited from class org.apache.commons.validator.routines.AbstractNumberValidator
CURRENCY_FORMAT, PERCENT_FORMAT, STANDARD_FORMAT -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a strict instance.CurrencyValidator(boolean strict, boolean allowFractions) Constructs an instance with the specified strict setting. -
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimalValidatorGets the singleton instance of this validator.protected ObjectParse the value with the specifiedFormat.Methods inherited from class org.apache.commons.validator.routines.BigDecimalValidator
isInRange, maxValue, minValue, processParsedValue, validate, validate, validate, validateMethods inherited from class org.apache.commons.validator.routines.AbstractNumberValidator
determineScale, getFormat, getFormat, getFormatType, isAllowFractions, isInRange, isValid, maxValue, minValue, parse
-
Constructor Details
-
CurrencyValidator
public CurrencyValidator()Constructs a strict instance. -
CurrencyValidator
Constructs an instance with the specified strict setting.- Parameters:
strict-trueif strictFormatparsing should be used.allowFractions-trueif fractions are allowed orfalseif integers only.
-
-
Method Details
-
getInstance
Gets the singleton instance of this validator.- Returns:
- A singleton instance of the CurrencyValidator.
-
parse
Parse the value with the specified
Format.This implementation is lenient whether the currency symbol is present or not. The default
NumberFormatbehavior 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.- Overrides:
parsein classAbstractFormatValidator- Parameters:
value- The value to be parsed.formatter- The Format to parse the value with.- Returns:
- The parsed value if valid or
nullif invalid.
-