PercentValidator (Apache Commons Validator 1.11.0 API)
Description: declaration: package: org.apache.commons.validator.routines, class: PercentValidator

# Class PercentValidator

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.PercentValidator

All Implemented Interfaces:
  `Serializable`

public class PercentValidator extends BigDecimalValidator
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 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 {`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 (that is, 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 (such as byte, short, integer, long and BigInteger) since percentages are converted to fractions (for example, `50%` is converted to `0.5`).

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
  `PercentValidator()`
  Constructs a strict instance.
  `PercentValidator(boolean strict)`
  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

  - ### PercentValidator

    public PercentValidator()
    Constructs a strict instance.

  - ### PercentValidator

    public PercentValidator(boolean strict)
    Constructs an instance with the specified strict setting.
    Parameters:
      `strict` - `true` if strict `Format` parsing should be used.

- ## Method Details

  - ### getInstance

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

  - ### 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.