添加类库:
jstl-1.2.jar、standard-1.1.2.jar
<%@ taglib uri="http://java.sun.com/jsp/jstl/ fmt" prefix="fmt " %>
一、<fmt:formatNumber>标记是用来设置数字,百分比和货币的格式。
<fmt:formatNumber>标记具有以下属性:
属性 | 描述 | 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 |
如果type属性是百分比或数字,那么你可以使用多种数字格式属性。maxIntegerDigits和minIntegerDigits属性允许你指定nonfractional部分大小的数字。如果实际数量超过maxIntegerDigits,则数字被截断。
属性也允许您确定应该使用多少位小数。minFractionalDigits和maxFractionalDigits属性允许您指定的小数位数。如果数量超过了最大数量的小数位数,数字将四舍五入。
分组可以用来插入逗号分隔千位组之间。指定分组,由设置的groupingIsUsed的属性为true或false。使用minIntegerDigits分组时,你必须小心地得到你想要的结果。
您可以选择使用模式的属性。这个属性让你指定你想您的号码编码包含特殊字符。下表显示了这些代码。
符号 | 描述 |
---|---|
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 - www.yiibai.com</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 |
二、fmt:formatDate 的输出格式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>fmt</title>
</head>
<body>
<fmt:formatNumber value="12" type="currency" pattern="$.00"/> ¥12.00
<br/>
<fmt:formatDate value="<%=new Date() %>" type="both"/> 2013-11-14 14:33:42
<br/>
<fmt:formatDate value="<%=new Date() %>" type="date"/> 2013-11-14
<br/>
<fmt:formatDate value="<%=new Date() %>" type="time"/> 14:35:02
<br/>
<fmt:formatDate value="<%=new Date() %>" type="date" dateStyle="default"/> 2013-11-14
<br/>
<fmt:formatDate value="<%=new Date() %>" type="date" dateStyle="short"/> 13-11-14
<br/>
<fmt:formatDate value="<%=new Date() %>" type="date" dateStyle="medium"/> 2013-11-14
<br/>
<fmt:formatDate value="<%=new Date() %>" type="date" dateStyle="long"/> 2013年11月14日
<br/>
<fmt:formatDate value="<%=new Date() %>" type="date" dateStyle="full"/> 2013年11月14日 星期四
<br/>
<fmt:formatDate value="<%=new Date() %>" type="time" timeStyle="default"/> 14:35:02
<br/>
<fmt:formatDate value="<%=new Date() %>" type="time" timeStyle="short"/> 下午2:35
<br/>
<fmt:formatDate value="<%=new Date() %>" type="time" timeStyle="medium"/> 14:37:36
<br/>
<fmt:formatDate value="<%=new Date() %>" type="time" timeStyle="long"/> 14:37:36 下午02时35分02秒
<br/>
<fmt:formatDate value="<%=new Date() %>" type="time" timeStyle="full"/> 下午02时35分02秒 CST
<br/>
<fmt:formatDate value="<%=new Date() %>" type="both" pattern="EEEE, MMMM d, yyyy HH:mm:ss Z"/>
星期四, 十一月 14, 2013 14:35:02 +0800
<br/>
<fmt:formatDate value="<%=new Date() %>" type="both" pattern="d MMM yy, h:m:s a zzzz" /> 14 十一月 13, 2:35:2 下午 中国标准时间
<br/>
<fmt:formatDate value="<%=new Date() %>" type="time" timeStyle="full" pattern ="yyyy-MM-dd HH:mm"/> 2013-11-14 14:35
<br/>
</body>
</html>