<Fmt: formatNumber> tag
<Fmt: formatNumber> tag is used to format numbers, percentages, currency.
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
<Fmt: formatNumber> tag has the following attributes:
Attributes | description | If necessary | Defaults |
---|---|---|---|
value | Figures to be displayed | Yes | no |
type | NUMBER, CURRENCY, or type PERCENT | no | Number |
pattern | Specify a custom formatting and output mode with | no | no |
currencyCode | Currency code (if type = "currency" time) | no | It depends on the default region |
currencySymbol | Currency symbol (if type = "currency" time) | no | It depends on the default region |
groupingUsed | Whether digital packet (TRUE or FALSE) | no | true |
maxIntegerDigits | The maximum number of integer digits | no | no |
minIntegerDigits | The minimum number of integer digits | no | no |
maxFractionDigits | The maximum number of bits after the decimal point | no | no |
minFractionDigits | The smallest decimal places | no | no |
var | Variable storage format numbers | no | Print to page |
scope | Scope var attribute | no | page |
If the type attribute percent or number, then you can use several other formatting numeric attributes. maxIntegerDigits minIntegerDigits properties and properties allow you to specify the length of integers. If the actual number exceeds the maximum maxIntegerDigits specified, then the number will be truncated.
Some attribute allows you to specify the number of decimal places. minFractionalDigits maxFractionalDigits properties and properties allow you to specify the number of decimal places. If the actual number exceeds the specified range, this number will be truncated.
Digital packet may be used to insert a comma at every three digits. groupingIsUsed attribute is used to specify whether to use digital packet. When used with minIntegerDigits property, it must be very careful to get the desired result.
You may use the pattern attribute. This property allows you to include the specified character at the time of digital codes. The following table lists these characters.
symbol | description |
---|---|
0 | Representative digit |
E | Exponential format |
# | A representative number, if not then display 0 |
. | Decimal point |
, | Digit grouping separators |
; | Delimited format |
- | Use the default negative prefix |
% | percentage |
? | Mille |
Currency symbol, instead of using the actual currency symbol | |
X | You can specify a character as a prefix or suffix |
' | Quote special characters in a prefix or suffix |
Examples Demo
<%@ 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 标签</title> </head> <body> <h3>数字格式化:</h3> <c:set var="balance" value="120000.2309" /> <p>格式化数字 (1): <fmt:formatNumber value="${balance}" type="currency"/></p> <p>格式化数字 (2): <fmt:formatNumber type="number" maxIntegerDigits="3" value="${balance}" /></p> <p>格式化数字 (3): <fmt:formatNumber type="number" maxFractionDigits="3" value="${balance}" /></p> <p>格式化数字 (4): <fmt:formatNumber type="number" groupingUsed="false" value="${balance}" /></p> <p>格式化数字 (5): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /></p> <p>格式化数字 (6): <fmt:formatNumber type="percent" minFractionDigits="10" value="${balance}" /></p> <p>格式化数字 (7): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /></p> <p>格式化数字 (8): <fmt:formatNumber type="number" pattern="###.###E0" value="${balance}" /></p> <p>美元 : <fmt:setLocale value="en_US"/> <fmt:formatNumber value="${balance}" type="currency"/></p> </body> </html>
Results are as follows:
数字格式化: 格式化数字 (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