<fmt:formatNumber> Tag

JSP 标准标签库JSP Standard Tag Library

The <fmt:formatNumber> tag is used to format numbers, percentages, and 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

The <fmt:formatNumber> tag has the following attributes:

Attribute Description Required Default Value
value Number to display Yes None
type NUMBER, CURRENCY, or PERCENT type no Number
pattern Specify a custom formatting pattern for output no None
currencyCode Currency code (when type="currency") no Depends on default locale
currencySymbol Currency symbol (when type="currency") no Depends on default locale
groupingUsed Whether to group numbers (TRUE or FALSE) no true
maxIntegerDigits Maximum number of integer digits no None
minIntegerDigits Minimum number of integer digits no None
maxFractionDigits Maximum number of fractional digits no None
minFractionDigits Minimum number of fractional digits 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, then you can use several other number formatting attributes. 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.

There are some attributes that allow you to specify the number of digits after the decimal point. The minFractionalDigits and maxFractionalDigits attributes allow you to specify the digits after the decimal point. If the actual number exceeds the specified range, the number will be truncated.

Digit grouping can be used to insert a comma every three digits. The groupingIsUsed attribute is used to specify whether digit grouping is used. When used together with the minIntegerDigits attribute, you must be very careful to obtain the expected result.

You may use the pattern attribute. This attribute allows you to include specified characters when formatting numbers. The following table lists these characters.

Symbol Description
0 Represents a digit
E Uses exponential format
# Represents a digit; displays 0 if none; leading zeros and trailing zeros are not displayed.
. Decimal point
, Digit grouping separator
; Separates formats
- Uses 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

<%@ 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>

The output is 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

JSP 标准标签库JSP Standard Tag Library

Other Extensions