fmt:formatNumbertag
fmt:formatNumberThe tag is used to format numbers, percentages, and currencies.
Syntax format
property
fmt:formatNumberThe tag has the following attributes:
| property | Description | Whether it is required | Default value |
|---|---|---|---|
| value | The number to display | Is | 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 | The maximum number of integer digits | No | None |
| minIntegerDigits | The minimum number of integer digits | No | None |
| maxFractionDigits | The maximum number of fractional digits | No | None |
| minFractionDigits | The minimum number of fractional digits | No | None |
| var | The variable that stores the formatted number | No | Print to page |
| scope | The scope of the var attribute | No | page |
If the type attribute is percent or number, you can use several other number-formatting attributes. The maxIntegerDigits and minIntegerDigits attributes allow you to specify the length of the integer. If the actual number exceeds the maximum 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 every three digits. The groupingIsUsed attribute specifies whether to use number grouping. When used together with the minIntegerDigits attribute, you must be careful to obtain the expected result.
You might use the pattern attribute. This attribute lets you include specified characters when encoding a number. The following table lists these characters.
| sign before positive numbers | Description |
|---|---|
| 0 | Represents a digit |
| E | Use exponential format |
| # | Represents a digit; if absent, 0 is displayed. Leading and trailing zeros are not displayed. |
| . | Decimal point |
| , | Number grouping separator |
| ; | Separator format |
| - | Use the default negative prefix |
| % | Percentage |
| ? | Per mille |
| ¤ | Currency symbol, replaced with the actual currency symbol |
| X | Specifies characters that can be used as a prefix or suffix |
| ' | Quotes special characters in a prefix or suffix |
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" %>
JSTL fmt:formatNumber 标签
数字格式化:
格式化数字 (1):
格式化数字 (2):
格式化数字 (3):
格式化数字 (4):
格式化数字 (5):
格式化数字 (6):
格式化数字 (7):
格式化数字 (8):
美元 :
The running result is as follows:
Number formatting:
格式化数字 (1): ¥120,000.23
格式化数字 (2): 000.231
格式化数字 (3): 120,000.231
格式化数字 (4): 120000.231
格式化数字 (5): 023%
格式化数字 (6): 12,000,023.0900000000%
格式化数字 (7): 023%
格式化数字 (8): 120E3
美元 : $120,000.23
JSP Standard Tag Library