Numbers are a generic notion used to quantify many things, such as age, salary, percent, currency, custom pattern, and so on. Also, we know that numbers can be integers, floats, doubles, and so on. Depending on what we represent, we know what kind of number to use and how to write it in the correct format and with the correct symbols attached. In this recipe you will see how to accomplish this task using JSF standard capabilities. For this we will take a generic double
number and we will output it to represent different things.
We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.
Converting numbers and applying basic formats to them are tasks that can be accomplished by the f:convertNumber
JSF converter. This converter can be customized using a set of attributes, listed next:
Attribute name |
Description |
---|---|
|
Represents the type of number. By default this type is set to |
|
Represents the decimal format pattern used to convert this number. |
|
Represents the locale to be used for displaying this number. The user's current locale is overridden. |
|
Represents the maximum number of integer digits to display. |
|
Represents the minimum number of integer digits to display. |
|
Represents the maximum number of fractional digits to display. |
|
Represents the minimum number of fractional digits to display. |
|
Represents a three-digit international currency code when the attribute |
|
Represents a symbol, like |
|
Set the value of this attribute to |
|
Set the value of this attribute to |
Now, let's suppose that we have the number 12345.12345 (five integer digits and five fraction digits). The following code will output this number using the f:convertNumber
converter and the previously listed attributes:
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <title>Standard converters for numbers - format numbers</title> </h:head> <h:body> <b><h:outputText value="-Formatting the double value 12345.12345-"/></b><br /> <!-- Format as 00000.00000 --> <h:outputText value="Format as 00000.00000: "/> <h:outputText value="#{numbersBean.doubleNumber}"> <f:convertNumber type="number" maxIntegerDigits="5" maxFractionDigits="5" groupingUsed="false"/> </h:outputText> <br /> <!-- Format as 00000 --> <h:outputText value="Format as 00000: "/> <h:outputText value="#{numbersBean.doubleNumber}"> <f:convertNumber type="number" maxIntegerDigits="5" maxFractionDigits="0"/> </h:outputText> <br /> <!-- Format as currency --> <h:outputText value="Format as currency: "/> <h:outputText value="#{numbersBean.doubleNumber}"> <f:convertNumber type="currency" currencySymbol="$" maxIntegerDigits="5" maxFractionDigits="2"/> </h:outputText> <br /> <!-- Format as percent --> <h:outputText value="Format as percent: "/> <h:outputText value="#{numbersBean.doubleNumber}"> <f:convertNumber type="percent" maxIntegerDigits="5" maxFractionDigits="5"/> </h:outputText> <br /> <!-- Format as pattern #####,00% --> <h:outputText value="Format as pattern #####,00%: "/> <h:outputText value="#{numbersBean.doubleNumber}"> <f:convertNumber pattern="#####,00%"/> </h:outputText> </h:body> </html>
The NumbersBean
is the managed bean, as shown next:
package numbers; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class NumbersBean { private double doubleNumber = 12345.12345; public double getDoubleNumber(){ return this.doubleNumber; } public void setDoubleNumber(double doubleNumber){ this.doubleNumber=doubleNumber; } }
The output will be as follows:
-Formatting the double value 12345.12345-
Format as 00000.00000: 12345.12345
Format as 00000: 12,345
Format as currency: $12,345.12
Format as percent: 34,512.345%
Format as pattern #####,00%: 1,23,45,12%
The number is displayed corresponding to the formatting attributes. The parts of the number that don't correspond to the conversion's restrictions are ignored or an error message is generated.
Notice that we have used the f:convertNumber
with the h:outputText
component, but you can follow the same logic to use with the h:inputText
component. These two components are the most used in conjunction with the f:convertNumber
converter.