The <fmt:formatNumber> tag is used to format numbers, percentages, and currencies.
Attribute
The <fmt:formatNumber> tag has the following attributes −
Attribute | Description | Required | Default |
---|---|---|---|
Value | Numeric value to display | Yes | None |
type | NUMBER, CURRENCY, or PERCENT | No | Number |
pattern | Specify a custom formatting pattern for the output. | No | None |
currencyCode | Currency code (for type = "currency") | No | From the default locale |
currencySymbol | Currency symbol (for type = "currency") | No | From the default locale |
groupingUsed | Whether to group numbers (TRUE or FALSE) | No | true |
maxIntegerDigits | Maximum number of integer digits to print | No | None |
minIntegerDigits | Minimum number of integer digits to print | No | None |
maxFractionDigits | Maximum number of fractional digits to print | No | None |
minFractionDigits | Minimum number of fractional digits to print | No | None |
var | Name of the variable to store the formatted number | No | Print to page |
scope | Scope of the variable to store the formatted number | No | page |
Example
<%@ taglib prefix = "c" uri = "https://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix = "fmt" uri = "https://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>JSTL fmt:formatNumber Tag</title> </head> <body> <h3>Number Format:</h3> <c:set var = "balance" value = "120000.2309" /> <p>Formatted Number (1): <fmt:formatNumber value = "${balance}" type = "currency"/></p> <p>Formatted Number (2): <fmt:formatNumber type = "number" maxIntegerDigits = "3" value = "${balance}" /></p> <p>Formatted Number (3): <fmt:formatNumber type = "number" maxFractionDigits = "3" value = "${balance}" /></p> <p>Formatted Number (4): <fmt:formatNumber type = "number" groupingUsed = "false" value = "${balance}" /></p> </body> </html>
The above code will generate the following result −
Number Format: Formatted Number (1): £120,000.23 Formatted Number (2): 000.231 Formatted Number (3): 120,000.231 Formatted Number (4): 120000.231