Class NumberConverter

java.lang.Object
jakarta.faces.convert.NumberConverter
All Implemented Interfaces:
PartialStateHolder, StateHolder, Converter

public class NumberConverter extends Object implements Converter, PartialStateHolder

Converter implementation for java.lang.Number values.

The getAsObject() method parses a String into an java.lang.Double or java.lang.Long, according to the following algorithm:

  • If the specified String is null, return a null. Otherwise, trim leading and trailing whitespace before proceeding.
  • If the specified String - after trimming - has a zero length, return null.
  • If the locale property is not null, use that Locale for managing parsing. Otherwise, use the Locale from the UIViewRoot.
  • If a pattern has been specified, its syntax must conform the rules specified by java.text.DecimalFormat. Such a pattern will be used to parse, and the type property will be ignored.
  • If a pattern has not been specified, parsing will be based on the type property, which expects a currency, a number, or a percent. The parse pattern for currencies, numbers, and percentages is determined by calling the getCurrencyInstance(), getNumberInstance(), or getPercentInstance() method of the java.text.NumberFormat class, passing in the selected Locale.
  • If the integerOnly property has been set to true, only the integer portion of the String will be parsed. See the JavaDocs for the setParseIntegerOnly() method of the java.text.NumberFormat class for more information.

The getAsString() method expects a value of type java.lang.Number (or a subclass), and creates a formatted String according to the following algorithm:

  • If the specified value is null, return a zero-length String.
  • If the specified value is a String, return it unmodified.
  • If the locale property is not null, use that Locale for managing formatting. Otherwise, use the Locale from the FacesContext.
  • If a pattern has been specified, its syntax must conform the rules specified by java.text.DecimalFormat. Such a pattern will be used to format, and the type property (along with related formatting options described in the next paragraph) will be ignored.
  • If a pattern has not been specified, formatting will be based on the type property, which formats the value as a currency, a number, or a percent. The format pattern for currencies, numbers, and percentages is determined by calling the percentages is determined by calling the getCurrencyInstance(), getNumberInstance(), or getPercentInstance() method of the java.text.NumberFormat class, passing in the selected Locale. In addition, the following properties will be applied to the format pattern, if specified:
    • If the groupingUsed property is true, the setGroupingUsed(true) method on the corresponding NumberFormat instance will be called.
    • The minimum and maximum number of digits in the integer and fractional portions of the result will be configured based on any values set for the maxFractionDigits, maxIntegerDigits, minFractionDigits, and minIntegerDigits properties.
    • If the type is set to currency, it is also possible to configure the currency symbol to be used, using either the currencyCode or currencySymbol properties. If both are set, the value for currencyCode takes precedence on a JDK 1.4 (or later) JVM; otherwise, the value for currencySymbol takes precedence.