The JSTL <fmt:formatNumber> Formatting Tag is used for formatting the numbers, currencies and percentages.
Syntax:
<fmt:formatNumber value=”val” otherattributes />
fmt:formatNumber tag attributes:
Attribute | Description | Required |
value | It specify the numeric value to display. | Yes |
type | NUMBER, CURRENCY, PERCENT | No |
pattern | It specify a custom formatting pattern for the output.. | No |
currencyCode | Currency code (for type=”currency”). | No |
currencySymbol | Currency symbol (for type=”currency”). | No |
groupingUsed | It specify whether to group numbers (TRUE or FALSE). | No |
maxIntegerDigits | It specify the maximum number of integer digits to print. | No |
minIntegerDigits | It specify the minimum number of integer digits to print. | No |
maxFractionDigits | It specify the maximum number of fractional digits to print. | No |
minFractionDigits | It specify the minimum number of fractional digits to print. | No |
var | It specify the name of the variable to store the formatted number. | No |
scope | It specify the scope of the variable to store the formatted number | No |
Example:
test.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>fmt:formatNumber JSTL formatting tag example</title> </head> <body> <c:set var="num" value="324123.23234"/> Number after setting type attribute: <br/> <fmt:formatNumber value="${num}" type="currency"/> <br/> Number after setting maxIntegerDigits and maxFractionDigits attribute: <br/> <fmt:formatNumber type="number" maxIntegerDigits="4" maxFractionDigits="3" value="${num}" /> <br/> Number after setting pattern attribute: <br/> <fmt:formatNumber type="number" pattern="##,###.##" value="${num}" /> </body> </html> |
web.xml
<web-app> <welcome-file-list> <welcome-file>test.jsp</welcome-file> </welcome-file-list> </web-app> |
Output:
Download this example.
Next Topic: JSTL fmt:parseNumber Formatting Tag with example.
Previous Topic: JSTL Formatting Tags with example.