JSTL Core <fmt:formatNumber> Tag

Advertisements


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

Attribute:

The <fmt:formatNumber> tag has following attributes:

AttributeDescription RequiredDefault
valueNumeric value to displayYesNone
typeNUMBER, CURRENCY, or PERCENTNoNumber
patternSpecify a custom formatting pattern for the output.NoNone
currencyCodeCurrency code (for type="currency")NoFrom the default locale
currencySymbolCurrency symbol (for type="currency")NoFrom the default locale
groupingUsedWhether to group numbers (TRUE or FALSE)Notrue
maxIntegerDigitsMaximum number of integer digits to printNoNone
minIntegerDigitsMinimum number of integer digits to printNoNone
maxFractionDigitsMaximum number of fractional digits to printNoNone
minFractionDigitsMinimum number of fractional digits to printNoNone
varName of the variable to store the formatted numberNoPrint to page
scopeScope of the variable to store the formatted numberNopage
  • If the type attribute is percent or number, then you can use several number-formatting attributes. The maxIntegerDigits and minIntegerDigits attributes allow you to specify the size of the nonfractional portion of the number. If the actual number exceeds maxIntegerDigits, then the number is truncated.

  • Attributes are also provided to allow you to determine how many decimal places should be used. The minFractionalDigits and maxFractionalDigits attributes allow you to specify the number of decimal places. If the number exceeds the maximum number of fractional digits, the number will be rounded.

  • Grouping can be used to insert commas between thousands groups. Grouping is specified by setting the groupingIsUsed attribute to either true or false. When using grouping with minIntegerDigits, you must be careful to get your intended result.

  • You may select to use the pattern attribute. This attribute lets you include special characters that specify how you would like your number encoded. Following table shows these codes.

SymbolDescription

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.

Example:

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

This would produce following result:

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



Advertisements
Advertisements