JSF - f:convertNumber



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.No 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 Application

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

Step Description
1 Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Application chapter.
2 Modify home.xhtml as explained below. Keep the rest of the files unchanged.
3 Compile and run the application to make sure business logic is working as per the requirements.
4 Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
5 Launch 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 the following result.

JSF f:convertNumber
jsf_convertor_tags.htm
Advertisements