The <fmt: formatNumber> tag is used to formatting the number data such as formatting numbers , currencies and percentages according to the customized formatting pattern.
The Syntax Of <fmt: formatNumber> Tag
<fmt: formatNumber attributes> body content </fmt:formatNumber >
Attributes of <fmt: formatNumber> Tag
maxFractionDigitsSpecifies the maximum number of digits in fractional portion.
Attributes | Description |
---|---|
value | Specifies the numeric value that has to be formatted. |
type | Specifies that the value has to be formatted as number, currency or percentage. |
pattern | Specifies the custom pattern to format the given value. |
var | Specifies the variable name to which the value is to be formatted. |
scope | The Scope into which the variable number has to be formatted. |
currencyCode | Specifies the code of currency. |
currencySymbol | Specifies the symbol of currency. |
groupingUsed | Specifies that whether the output value is to be grouped by using comma or seperator in the output. |
maxIntegerDigits | Specifies the maximum number of digits. |
minIntegerDigits | Specifies the minimum number of digits. |
minFractionDigits | Specifies the minimum number of digits in fractional portion. |
Example
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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>formatNumber Tag</title> </head> <body> <h3>Number Format:</h3> <c:set var="balance" value="15705.79040" /> <p> Currency: <fmt:formatNumber value="${balance}" type="currency" /> </p> <p> Grouping : <fmt:formatNumber type="number" groupingUsed="true" value="${balance}" /> </p> <p> Max Integer Digit: <fmt:formatNumber type="number" maxIntegerDigits="2" value="${balance}" /> </p> <p> Max Fraction Digit: <fmt:formatNumber type="number" maxFractionDigits="5" value="${balance}" /> </p> <p> Max Integer Digits using percent: <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /> </p> <p> Min Fraction Digits using percent: <fmt:formatNumber type="percent" minFractionDigits="5" value="${balance}" /> </p> <p> Min Integer Digit: <fmt:formatNumber type="number" minIntegerDigits="2" value="${balance}" /> </p> <p> Using Pattern: <fmt:formatNumber type="number" pattern="###.###$" value="${balance}" /> </p> </body> </html>
Details of the Code
- <fmt:formatNumber value=”${balance}” type=”currency”/> tag is used to display the value with currency symbol.
- <fmt:formatNumber type=”number” groupingUsed=”true” value=”${balance}” /> is used to group the digits with comma and in code we have done true so there is grouping used in the digits.
- <fmt:formatNumber type=”number” maxIntegerDigits=”2″ value=”${balance}” /> tag is used for maximum integer number to be displayed.
- <fmt:formatNumber type=”number” maxFractionDigits=”5″ value=”${balance}” /> tag is used to display maximum fractional digit.
- <fmt:formatNumber type=”percent” maxIntegerDigits=”3″ value=”${balance}” /> tag is used for maximum integer number to be displayed with percentage.
- <fmt:formatNumber type=”percent” minFractionDigits=”5″ value=”${balance}” /> tag is used to display minimum fractional digit.
- <fmt:formatNumber type=”number” pattern=”###.###$” value=”${balance}” /> is used to format the value in a custom pattern.
Steps for Execution
- Save this file as example.jsp in your eclipse IDE.
- Now select this jsp file, right mouse click and select Run as ->Run on server
Output:
When the execution process is completed successfully we will get the following output :
Previous Tutorial : JSTL Format fmt:setBundle Tag :: Next Tutorial : JSTL Format fmt:parseNumber Tag