# Class CurrencyValidator

java.lang.Object
org.apache.commons.validator.routines.AbstractFormatValidator
org.apache.commons.validator.routines.AbstractNumberValidator
org.apache.commons.validator.routines.BigDecimalValidator
org.apache.commons.validator.routines.CurrencyValidator

All Implemented Interfaces:
  `Serializable`

public class CurrencyValidator extends BigDecimalValidator
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 `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:
  - Serialized Form

- ## Field Summary

  ### Fields inherited from class AbstractNumberValidator

  `CURRENCY_FORMAT, PERCENT_FORMAT, STANDARD_FORMAT`
  Modifier and Type
  Field
  Description
  `static final int`
  `CURRENCY_FORMAT`
  Currency `NumberFormat` type.
  `static final int`
  `PERCENT_FORMAT`
  Percent `NumberFormat` type.
  `static final int`
  `STANDARD_FORMAT`
  Standard `NumberFormat` type.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CurrencyValidator()`
  Constructs a strict instance.
  `CurrencyValidator(boolean strict, boolean allowFractions)`
  Constructs an instance with the specified strict setting.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static BigDecimalValidator`
  `getInstance()`
  Gets the singleton instance of this validator.
  `protected Object`
  `parse(String value, Format formatter)`
  Parse the value with the specified `Format`.

  ### Methods inherited from class BigDecimalValidator

  `getFormat, isInRange, maxValue, maxValue, minValue, minValue, processParsedValue, validate, validate, validate, validate`
  Modifier and Type
  Method
  Description
  `protected Format`
  `getFormat(String pattern, Locale locale)`
  Returns a `Format` that parses to a `BigDecimal` so the exact value of the input is preserved.
  `boolean`
  `isInRange(BigDecimal value, double min, double max)`
  Tests if the value is within a specified range.
  `boolean`
  `maxValue(Number value, Number max)`
  Tests if the value is less than or equal to a maximum, comparing the exact values.
  `boolean`
  `maxValue(BigDecimal value, double max)`
  Tests if the value is less than or equal to a maximum.
  `boolean`
  `minValue(Number value, Number min)`
  Tests if the value is greater than or equal to a minimum, comparing the exact values.
  `boolean`
  `minValue(BigDecimal value, double min)`
  Tests if the value is greater than or equal to a minimum.
  `protected Object`
  `processParsedValue(Object value, Format formatter)`
  Converts the parsed value to a `BigDecimal`.
  `BigDecimal`
  `validate(String value)`
  Validates and converts a `BigDecimal` using the default `Locale`.
  `BigDecimal`
  `validate(String value, String pattern)`
  Validates and converts a `BigDecimal` using the specified pattern.
  `BigDecimal`
  `validate(String value, String pattern, Locale locale)`
  Validates and converts a `BigDecimal` using the specified pattern and/ or `Locale`.
  `BigDecimal`
  `validate(String value, Locale locale)`
  Validates and converts a `BigDecimal` using the specified `Locale`.

  ### Methods inherited from class AbstractNumberValidator

  `determineScale, getFormat, getFormatType, isAllowFractions, isInRange, isValid, parse`
  Modifier and Type
  Method
  Description
  `protected int`
  `determineScale(NumberFormat format)`
  Returns the multiplier of the `NumberFormat`.
  `protected Format`
  `getFormat(Locale locale)`
  Gets the `NumberFormat` for the specified Locale.
  `int`
  `getFormatType()`
  Gets the type of `NumberFormat` created by this validator instance.
  `boolean`
  `isAllowFractions()`
  Tests whether the number being validated is a decimal or integer.
  `boolean`
  `isInRange(Number value, Number min, Number max)`
  Tests if the value is within a specified range.
  `boolean`
  `isValid(String value, String pattern, Locale locale)`
  Tests validity using the specified `Locale`.
  `protected Object`
  `parse(String value, String pattern, Locale locale)`
  Parses the value using the specified pattern.

  ### Methods inherited from class AbstractFormatValidator

  `format, format, format, format, format, isStrict, isValid, isValid, isValid`
  Modifier and Type
  Method
  Description
  `String`
  `format(Object value)`
  Formats an object into a `String` using the default Locale.
  `String`
  `format(Object value, String pattern)`
  Formats an object into a `String` using the specified pattern.
  `String`
  `format(Object value, String pattern, Locale locale)`
  Formats an object using the specified pattern and/or `Locale`.
  `protected String`
  `format(Object value, Format formatter)`
  Formats a value with the specified `Format`.
  `String`
  `format(Object value, Locale locale)`
  Formats an object into a `String` using the specified Locale.
  `boolean`
  `isStrict()`
  Tests whether validated values should adhere strictly to the `Format` used.
  `boolean`
  `isValid(String value)`
  Validates using the default `Locale`.
  `boolean`
  `isValid(String value, String pattern)`
  Validates using the specified pattern.
  `boolean`
  `isValid(String value, Locale locale)`
  Validates using the specified `Locale`.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### CurrencyValidator

    public CurrencyValidator()
    Constructs a strict instance.

  - ### CurrencyValidator

    public CurrencyValidator(boolean strict, boolean allowFractions)
    Constructs an instance with the specified strict setting.
    Parameters:
      `strict` - `true` if strict `Format` parsing should be used.
      `allowFractions` - `true` if fractions are allowed or `false` if integers only.

- ## Method Details

  - ### getInstance

    public static BigDecimalValidator getInstance()
    Gets the singleton instance of this validator.
    Returns:
      A singleton instance of the CurrencyValidator.

  - ### 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` behavior 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:
      `parse` in class `AbstractFormatValidator`
    Parameters:
      `value` - The value to be parsed.
      `formatter` - The Format to parse the value with.
    Returns:
      The parsed value if valid or `null` if invalid.