f:convertNumber tag is used to convert a string value to a number of required format.
The following code shows how to use f:convertNumber tag
<f:convertNumber minFractionDigits="2" />
Attribute | Description |
---|---|
type | number (default), currency , or percent |
pattern | Formatting pattern, as defined in java.text.DecimalFormat |
maxFractionDigits | Maximum number of digits in the fractional part |
minFractionDigits | Minimum number of digits in the fractional part |
maxIntegerDigits | Maximum number of digits in the integer part |
minIntegerDigits | Minimum number of digits in the integer part |
integerOnly | True if only the integer part is parsed (default: false) |
groupingUsed | True if grouping separators are used (default: true) |
locale | Locale whose preferences are to be used for parsing and formatting |
currencyCode | ISO 4217 currency code to use when converting currency values |
currencySymbol | Currency symbol to use when converting currency values |
The following code is from demo.xhtml.
<?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:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core" > <h:body> <h:form> <h:panelGrid columns="3"> Amount : <h:inputText id="amount" value="#{receipt.amount}" size="20" required="true" label="Amount" > <!-- display in at least 2 decimal points --> <f:convertNumber minFractionDigits="2" /> </h:inputText> <h:message for="amount" style="color:red" /> </h:panelGrid> <h:commandButton value="Submit" action="result" /> </h:form> </h:body> </html>
The following code is from UserBean.java.
package com.java2s.common; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="receipt") @SessionScoped public class UserBean implements Serializable{ double amount; public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } }
The following code is from result.xhtml.
<?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:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core" > <h:body> <p> Amount [minFractionDigits="2"] : <h:outputText value="#{receipt.amount}" > <f:convertNumber minFractionDigits="2" /> </h:outputText> </p> <p> Amount [pattern="#0.000"] : <h:outputText value="#{receipt.amount}" > <f:convertNumber pattern="#0.000" /> </h:outputText> </p> <p> Amount [currencySymbol="$"] : <h:outputText value="#{receipt.amount}"> <f:convertNumber currencySymbol="$" type="currency" /> </h:outputText> </p> <p> Amount [currencyCode="GBP"] : <h:outputText value="#{receipt.amount}" > <f:convertNumber currencyCode="GBP" type="currency" /> </h:outputText> </p> <p> Amount [type="percent"] : <h:outputText value="#{receipt.amount}" > <f:convertNumber type="percent" /> </h:outputText> </p> </h:body> </html>
Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.
After Tomcat finish starting, type the following URL in the browser address bar.
http://localhost:8080/simple-webapp/demo.xhtml