In this tutorial you will learn about the JSTL fmt formatNumber tag in JSP.
JSTL fmt Tag formatNumber Example
In this tutorial you will learn about the JSTL fmt formatNumber tag in JSP.
In JSTL Numeric values as a number, percentages, and currencies are formatted using the <fmt:formatNumber> tag.
Attributes of <fmt:formatNumber>
- value : This is an optional attribute. Value of this attribute is provided as numeric value to be formatted.
- type : This is an optional attribute specifies into which type of formatting you required (number, currency, or percentage).
- pattern : This is an optional attribute that specifies the custom formatting pattern.
- currencyCode : This is an optional attribute that uses the ISO 4217 currency code, and is used only when the 'type' attribute is specified to currencies.
- currencySymbol : This is an optional attribute that specifies the symbol of currencies and is used only when the 'type' attribute is specified to currencies.
- groupingUsed : This is an optional attribute and defines that the formatted output will contain any grouping separators or not.
- maxIntegerDigits : This is an optional attribute that specifies the (maximum ) how many digits an integer part will contain in the formatted output.
- minIntegerDigits : This is an optional attribute that specifies the (minimum ) how many digits an integer part will contain in the formatted output.
- maxFractionDigits : This is an optional attribute that specifies the (maximum ) how many digits the fraction part will contain in the formatted output.
- minFractionDigits : This is an optional attribute that specifies the (minimum ) how many digits the fraction part will contain in the formatted output.
- var : This is an optional attribute that defines the name of a scoped variable, kept the formatted result in the String form.
- scope : This is an optional attribute that may be used for specifying the scope of the attribute 'var'.
Example :
Here an example is being given which will demonstrate you how to use the JSTL <fmt:formatNumber> tag in JSP page. In this example I have created a JSP page into which I have used the various number formatting schemes by using the various attributes of the <fmt:formatNumber> tag.
JstlFmtFormatNumber.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>JSTL fmt:formatNumber Tag</title> </head> <body> <h3>Number Formatting Schemes ..........</h3> <p><b>Number to Currency :</b> <fmt:formatNumber type="currency" value="12345.6789"/></p> <p><b>Max Number of Integer Digits :</b> <fmt:formatNumber type="number" value="12345.6789" maxIntegerDigits="5" /></p> <p><b>Max Number of Fraction Digits : </b> <fmt:formatNumber type="number" value="12345.6789" maxFractionDigits="1" /></p> <p><b>Number grouping used (false): </b> <fmt:formatNumber type="number" value="12345.6789" groupingUsed="false" /></p> <p><b>Number grouping used (true): </b> <fmt:formatNumber type="number" value="12345.6789" groupingUsed="true" /></p> <h3>Number to Percent :</h3> <p><b>Change number to percent : </b> <fmt:formatNumber type="percent" value="12345.6789" /></p> <p><b>Max Number of Integer Digits :</b> <fmt:formatNumber type="percent" value="12345.6789" maxIntegerDigits="3" /></p> <p><b>Min Number of Fraction Digits : </b> <fmt:formatNumber type="percent" value="12345.6789" minFractionDigits="3" /></p> <p><b>Number Formatting using pattern :</b> <fmt:formatNumber type="number" value="12345.6789" pattern="###.##E0" /></p> </body> </html>
How to run this example
Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :
- jstl.jar
- standard.jar
After adding of these jar files you may execute your program in the following ways :
- Select JstlFmtFormatNumber.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- On the Eclipse Editor go to your JstlFmtFormatNumber.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
- Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
- A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish
NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.
Output :
When the execution process will be completed successfully an output will be displayed on your eclipse browser as :
[ 0 ] Comments