JSF - f:convertNumber


Advertisements


f:convertNumber tag is used to convert a string value to a number of required format.

JSF Tag

<f:convertNumber minFractionDigits="2" />

Tag Attributes

S.N.Attribute & Description
1type

number (default), currency , or percent

2pattern

Formatting pattern, as defined in java.text.DecimalFormat

3maxFractionDigits

Maximum number of digits in the fractional part

4minFractionDigits

Minimum number of digits in the fractional part

5maxIntegerDigits

Maximum number of digits in the integer part

6minIntegerDigits

Minimum number of digits in the integer part

7integerOnly

True if only the integer part is parsed (default: false)

8groupingUsed

True if grouping separators are used (default: true)

9locale

Locale whose preferences are to be used for parsing and formatting

10currencyCode

ISO 4217 currency code to use when converting currency values

11currencySymbol

Currency symbol to use when converting currency values

Example Application

Let us create a test JSF application to test the above tag.

StepDescription
1Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Application chapter.
2Modify home.xhtml as explained below. Keep rest of the files unchanged.
3Compile and run the application to make sure business logic is working as per the requirements.
4Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
5Launch your web application using appropriate URL as explained below in the last step.

home.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">
   <h:head>
      <title>JSF tutorial</title>			
   </h:head>
   <h:body>
      <h2>ConvertNumber Example</h2>
      <table border="1" cellspacing="2" cellpadding="2">
         <tr><th>Parameter</th><th>Value Passed</th><th>Output</th></tr>
         <tr><td>minFractionDigits="2"</td><td>100.12345</td>
         <td>
            <h:outputText value="100.12345" >
               <f:convertNumber minFractionDigits="2" />
            </h:outputText>
         </td></tr>
         <tr><td>pattern="#000.000"</td><td>100.12345</td>
         <td>
            <h:outputText value="100.12345" >
               <f:convertNumber pattern="#000.000" />
            </h:outputText>
         </td></tr>
         <tr><td>currencySymbol="$"</td><td>$100</td>
         <td>
            <h:outputText value="$100">
               <f:convertNumber currencySymbol="$" type="currency" />
            </h:outputText>
         </td></tr>
         <tr><td>type="percent"</td><td>100.12345%</td>
         <td>
            <h:outputText value="100.12345%" >
               <f:convertNumber type="percent" />
            </h:outputText>
         </td></tr>
      </table>
   </h:body>
</html>

Once you are ready with all the changes done, let us compile and run the application as we did in JSF - First Application chapter. If everything is fine with your application, this will produce following result:

JSF f:convertNumber
jsf_convertor_tags.htm

Advertisements