English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

<fmt:formatNumber> tag

JSP Standard Tag Library

<fmt:formatNumber> tag is used to format numbers, percentages, and currencies.

Syntax format

<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>"/>

Attribute

<fmt:formatNumber> tag has the following attributes:

Attribute Description Is it necessary Default value
value The number to be displayed 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 the default region
currencySymbol Currency symbol (when type="currency") No Depends on the default region
groupingUsed Whether to group numbers (TRUE or FALSE) No true
maxIntegerDigits Maximum number of digits for integer No None
minIntegerDigits Minimum number of digits for integer No None
maxFractionDigits Maximum number of digits after the decimal point No None
minFractionDigits Minimum number of digits after the decimal point No None
var Store the formatted number variable No Print to page
scope The scope of the var attribute No page

If the type attribute is percent or number, you can use other formatting number properties. The maxIntegerDigits attribute and minIntegerDigits attribute allow you to specify the length of the integer. If the actual number exceeds the maximum value specified by maxIntegerDigits, the number will be truncated.

Some properties allow you to specify the number of digits after the decimal point. The minFractionalDigits attribute and maxFractionalDigits attribute 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 numbers. The groupingIsUsed attribute is used to specify whether number grouping is used. When used with the minIntegerDigits attribute, it is necessary to be very careful to obtain the expected results.

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

Symbol Description
0 Represents one digit
E Use exponential format
# Represents one digit, if not, displays 0, leading 0 and trailing 0 are not displayed.
. Decimal point
, Number grouping separator
; Separator format
- Use default negative number prefix
% Percentage
? Thousand separator
¤ Currency symbol, replace with the actual currency symbol
X Specify characters that can be used as prefix or suffix
' Refer to special characters in 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" %>
<html>
<head>
  <title>JSTL fmt:formatNumber Tag</title>
</head>
<body>
<h2>Number Formatting:</h2>
<c:set var="balance" value="120000.2309" />
<p>Format Numbers (1): <fmt:formatNumber value="${balance}" 
            type="currency"/></p>
<p>Format Numbers (2): <fmt:formatNumber type="number" 
            maxIntegerDigits="3" value="${balance}" /></p>
<p>Format Numbers (3): <fmt:formatNumber type="number" 
            maxFractionDigits="3" value="${balance}" /></p>
<p>Format Numbers (4): <fmt:formatNumber type="number" 
            groupingUsed="false" value="${balance}" /></p>
<p>Format Numbers (5): <fmt:formatNumber type="percent" 
            maxIntegerDigits="3" value="${balance}" /></p>
<p>Format Numbers (6): <fmt:formatNumber type="percent" 
            minFractionDigits="10" value="${balance}" /></p>
<p>Format Numbers (7): <fmt:formatNumber type="percent" 
            maxIntegerDigits="3" value="${balance}" /></p>
<p>Format Numbers (8): <fmt:formatNumber type="number" 
            pattern="###.###E0" value="${balance}" /></p>
<p>US Dollar :
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/></p>
</body>
</html>

The running result is as follows:

Number Formatting:
Format Numbers (1): ¥120,000.23
Format Numbers (2): 000.231
Format Numbers (3) : 120,000.231
Format Numbers (4) : 120000.231
Format Numbers (5): 023%
Format Numbers (6) : 12,000,023.0900000000%
Format Numbers (7): 023%
Format Numbers (8) : 120E3
US Dollar : $120,000.23

JSP Standard Tag Library