Jsf convertnumber example

JSF f convertnumber tag:

JSF f:convertNumber tag is a standard JSF converter which is used to converts a string into a number of specific format. It is also used for valid number validation.

JSF converter tag attributes:

Attribute Description
1. type number (default), currency , or percent
2. pattern Formatting pattern, as defined in java.text.DecimalFormat
3. maxFractionDigits Maximum number of digits in the fractional part
4. minFractionDigits Minimum number of digits in the fractional part
5. maxIntegerDigits Maximum number of digits in the integer part
6. minIntegerDigits Minimum number of digits in the integer part
7. integerOnly True if only the integer part is parsed (default: false)
8. groupingUsed True if grouping separators are used (default: true)
9. locale Locale whose preferences are to be used for parsing and formatting
10. currencyCode ISO 4217 currency code to use when converting currency values
11. currencySymbol Currency symbol to use when converting currency values

Example:

test.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:ui="http://java.sun.com/jsf/facelets">
	<h:head>
		<title>JSF f convert number example.</title> 
	</h:head>
    <h:body> 
      <h3>JSF f convert number example.</h3>
      <h:form>
        <h:inputText label="Enter Amount: " 
           value="#{test.amount}" required="true">
         <f:convertNumber minFractionDigits="2"/>
        </h:inputText>
        <h:commandButton action="welcome.xhtml" value="Submit"/>
      </h:form>
    </h:body>
 
</html>

Test.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
/**
 * Managed bean.
 * @author w3schools
 */
@ManagedBean(name="test")
@SessionScoped
public class Test {
	private double amount;
 
	public double getAmount() {
		return amount;
	}
 
	public void setAmount(double amount) {
		this.amount = amount;
	}
}

welcome.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:ui="http://java.sun.com/jsf/facelets">
	<h:head>
		<title>JSF f convert number example.</title>
	</h:head>
    <h:body> 
      <h3>JSF f convert number example.</h3>
      Amount minFractionDigits="2": 
      <h:outputText value="#{test.amount}" >
        <f:convertNumber minFractionDigits="2" />
      </h:outputText>
      <br/>
      Amount pattern="#0.000": 
      <h:outputText value="#{test.amount}" >
        <f:convertNumber pattern="#0.000" />
      </h:outputText>
      <br/>
      Amount currencySymbol="$": 
      <h:outputText value="#{test.amount}" >
        <f:convertNumber currencySymbol="$" type="currency" />
      </h:outputText>
      <br/>
      Amount type="percent": 
      <h:outputText value="#{test.amount}" >
        <f:convertNumber type="percent" />
      </h:outputText>
    </h:body>
 
</html>

faces-config.xml

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="2.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xi="http://www.w3.org/2001/XInclude" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>
         javax.faces.webapp.FacesServlet
	<servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
 
</web-app>

URL:

http://localhost:7001/JSFExample29/faces/test.xhtml

Output:

JSF example29-1
 
Enter any number value.
JSF example29-2
 
Click Submit button.
JSF example29-3
 
Download this example.