<fmt:formatNumber> Tag
The <fmt:formatNumber> tag is used to format numbers, percentages, and currencies.
Syntax
<fmt:formatNumber
value="<string>"
type="<string>"
pattern="<string>"
currencyCode="<string>"
currencySymbol="<string>"
groupingUsed="<string>"
maxIntegerDigits="<string>"
minIntegerDigits="<string>"
maxFractionDigits="<string>"
minFractionDigits="<string>"
var="<string>"
scope="<string>"/>
Attributes
The <fmt:formatNumber> tag has the following attributes:
Attribute | Description | Required | Default Value |
---|---|---|---|
value | The number to be displayed | Yes | None |
type | NUMBER, CURRENCY, or PERCENT type | No | Number |
pattern | Specifies a custom formatting pattern for the output | No | None |
currencyCode | Currency code (when type="currency") | No | Depends on the default locale |
currencySymbol | Currency symbol (when type="currency") | No | Depends on the default locale |
groupingUsed | Whether to group numbers (TRUE or FALSE) | No | true |
maxIntegerDigits | Maximum number of digits in the integer part | No | None |
minIntegerDigits | Minimum number of digits in the integer part | No | None |
maxFractionDigits | Maximum number of digits after the decimal point | No | None |
minFractionDigits | Minimum number of digits after the decimal point | No | None |
var | Variable to store the formatted number | No | Print to page |
scope | Scope of the var attribute | No | page |
If the type attribute is percent or number, you can use several other attributes to format the number. The maxIntegerDigits and minIntegerDigits attributes allow you to specify the length of the integer part. If the actual number exceeds the maximum value specified by maxIntegerDigits, the number will be truncated.
Some attributes allow you to specify the number of digits after the decimal point. The minFractionalDigits and maxFractionalDigits attributes allow you to specify the number of digits after the decimal point. If the actual number exceeds the specified range, the number will be truncated.
Number grouping can be used to insert a comma between every three digits. The groupingIsUsed attribute is used to specify whether to use number grouping. When used together with the minIntegerDigits attribute, care must be taken to achieve the desired result.
You might use the pattern attribute. This attribute allows you to include specified characters when encoding the number. The following table lists these characters.
Symbol | Description |
---|---|
0 | Represents a digit |
E | Uses exponential format |
# | Represents a digit, displays 0 if none, leading and trailing 0s are not displayed |
. | Decimal point |
, | Number grouping separator |
; | Separates formats |
- | Uses the default negative prefix |
% | Percentage |
? | Per mille |
¤ | Currency symbol, replaced by the actual currency symbol |
X | Specifies characters that can be used as a prefix or suffix |
' | Quotes special characters in prefixes or suffixes |
Example Demonstration
<%@ 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" %>
<html>
<head>
<title>JSTL fmt:formatNumber Tag</title>
</head>
<body>
<h3>Number Formatting:</h3>
<c:set var="balance" value="120000.2309" />
English:
<p>Formatted Number (1):
Output:
Number Formatting:
Formatted Number (1): ¥120,000.23
Formatted Number (2): 000.231
Formatted Number (3): 120,000.231
Formatted Number (4): 120000.231
Formatted Number (5): 023%
Formatted Number (6): 12,000,023.0900000000%
Formatted Number (7): 023%
Formatted Number (8): 120E3
US Dollar: $120,000.23
---
[JSP Standard Tag Library](jsp-jstl.html)