You can convert a component’s data to a java.lang.Number by nesting the convertNumber tag inside the component tag. The convertNumber tag has several attributes that allow you to specify the format and type of the data. Table 8–2 lists the attributes.
The following example uses a convertNumber tag to display the total prices of the contents of a shopping cart:
<h:outputText value="#{cart.total}" > <f:convertNumber type="currency"/> </h:outputText>
When binding the NumberConverter to a component, ensure that the backing bean property to which the component is bound is of a primitive type or has a type of java.lang.Number. In the preceding example, cart.total is of type java.lang.Number.
Here is an example of a number that this tag can display:
$934 |
This result can also be displayed by using the following tag, where the currency pattern is specified:
<h:outputText id="cartTotal" value="#{cart.Total}" > <f:convertNumber pattern="$####" /> </h:outputText>
See the “Customizing Formats” lesson of the Java Tutorial at http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html for more information on how to format the output by using the pattern attribute of the convertNumber tag.
Table 8–2 Attributes for the convertNumber Tag
Attribute |
Type |
Description |
---|---|---|
binding |
NumberConverter |
Used to bind a converter to a backing bean property. |
currencyCode |
String |
ISO 4217 currency code, used only when formatting currencies. |
currencySymbol |
String |
Currency symbol, applied only when formatting currencies. |
for |
String |
Used with composite components. Refers to one of the objects within the composite component inside which this tag is nested. |
groupingUsed |
Boolean |
Specifies whether formatted output contains grouping separators. |
integerOnly |
Boolean |
Specifies whether only the integer part of the value will be parsed. |
locale |
String or Locale |
Locale whose number styles are used to format or parse data. |
maxFractionDigits |
int |
Maximum number of digits formatted in the fractional part of the output. |
maxIntegerDigits |
int |
Maximum number of digits formatted in the integer part of the output. |
minFractionDigits |
int |
Minimum number of digits formatted in the fractional part of the output. |
minIntegerDigits |
int |
Minimum number of digits formatted in the integer part of the output. |
pattern |
String |
Custom formatting pattern that determines how the number string is formatted and parsed. |
type |
String |
Specifies whether the string value is parsed and formatted as a number, currency, or percentage. If not specified, number is used. |