发布于 2015-08-17 14:55:18 | 250 次阅读 | 评论: 0 | 来源: 网络整理
<fmt:formatNumber>标签用于格式化数字,百分比和货币。
<fmt:formatNumber>标签具有以下属性:
属性 | 描述 | 必须? | 默认 |
---|---|---|---|
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 |
如果type属性是百分比或数字,那么您可以使用多个数字格式属性。maxIntegerDigits和minIntegerDigits属性允许你指定数量的nonfractional部分的大小。如果实际数目超过maxIntegerDigits,电话号码将被截断。
还提供了属性,让你确定有多少小数位应该被使用。该minFractionalDigits和maxFractionalDigits属性允许你指定的小数位数。如果数目超过小数的最大位数,该数字将四舍五入。
分组可以用来插入数千组逗号之间。分组是通过设置groupingIsUsed属性为true或false指定。当使用minIntegerDigits分组,要小心得到预期的结果。
你可以选择要使用的模式属性。这个属性可指定希望的格式,如电话号码编码特殊字符。下表显示了这些代码。
Symbol | 描述 |
---|---|
0 |
Represents a digit. |
E |
Represents in exponential form. |
# |
Represents a digit; displays 0 as absent. |
. |
Serves as a placeholder for a decimal separator. |
, |
Serves as a placeholder for a grouping separator. |
; |
Separates formats. |
- |
Used as the default negative prefix. |
% |
Multiplies by 100 and displays as a percentage. |
? |
Multiplies by 1000 and displays as per mille. |
¤ |
Represents the currency sign; replaced by actional currency symbol. |
X |
Indicates that any other characters can be used in the prefix or suffix. |
' |
Used to quote special characters in a prefix or suffix. |
<%@ 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 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>
<p>Formatted Number (5): <fmt:formatNumber type="percent"
maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (6): <fmt:formatNumber type="percent"
minFractionDigits="10" value="${balance}" /></p>
<p>Formatted Number (7): <fmt:formatNumber type="percent"
maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (8): <fmt:formatNumber type="number"
pattern="###.###E0" value="${balance}" /></p>
<p>Currency in USA :
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/></p>
</body>
</html>
这将产生以下结果:
NUMBER FORMAT: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 Currency in USA : $120,000.23 |