JSTL Format fmt:formatNumber Tag

The <fmt: formatNumber> tag is used to formatting the number data such as formatting numbers , currencies and percentages according to the customized formatting pattern.

The Syntax Of <fmt: formatNumber> Tag

<fmt: formatNumber attributes> body content </fmt:formatNumber >

Attributes of <fmt: formatNumber> Tag

maxFractionDigitsSpecifies the maximum number of digits in fractional portion.

valueSpecifies the numeric value that has to be formatted.
typeSpecifies that the value has to be formatted as number, currency or percentage.
patternSpecifies the custom pattern to format the given value.
varSpecifies the variable name to which the value is to be formatted.
scopeThe Scope into which the variable number has to be formatted.
currencyCodeSpecifies the code of currency.
currencySymbolSpecifies the symbol of currency.
groupingUsedSpecifies that whether the output value is to be grouped by using comma or seperator in the output.
maxIntegerDigitsSpecifies the maximum number of digits.
minIntegerDigitsSpecifies the minimum number of digits.
minFractionDigitsSpecifies the minimum number of digits in fractional portion.


<%@ page language="java" contentType="text/html; charset=UTF-8"
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>formatNumber Tag</title>

	<h3>Number Format:</h3>

	<c:set var="balance" value="15705.79040" />

		<fmt:formatNumber value="${balance}" type="currency" />

		Grouping :
		<fmt:formatNumber type="number" groupingUsed="true" value="${balance}" />

		Max Integer Digit:
		<fmt:formatNumber type="number" maxIntegerDigits="2"
			value="${balance}" />

		Max Fraction Digit:
		<fmt:formatNumber type="number" maxFractionDigits="5"
			value="${balance}" />

		Max Integer Digits using percent:
		<fmt:formatNumber type="percent" maxIntegerDigits="3"
			value="${balance}" />

		Min Fraction Digits using percent:
		<fmt:formatNumber type="percent" minFractionDigits="5"
			value="${balance}" />

		Min Integer Digit:
		<fmt:formatNumber type="number" minIntegerDigits="2"
			value="${balance}" />

		Using Pattern:
		<fmt:formatNumber type="number" pattern="###.###$" value="${balance}" />


Details of the Code

  • <fmt:formatNumber value=”${balance}” type=”currency”/> tag is used to display the value with currency symbol.
  • <fmt:formatNumber type=”number” groupingUsed=”true” value=”${balance}” /> is used to group the digits with comma and in code we have done true so there is grouping used in the digits.
  • <fmt:formatNumber type=”number” maxIntegerDigits=”2″ value=”${balance}” /> tag is used for maximum integer number to be displayed.
  • <fmt:formatNumber type=”number” maxFractionDigits=”5″ value=”${balance}” /> tag is used to display maximum fractional digit.
  • <fmt:formatNumber type=”percent” maxIntegerDigits=”3″ value=”${balance}” /> tag is used for maximum integer number to be displayed with percentage.
  • <fmt:formatNumber type=”percent” minFractionDigits=”5″ value=”${balance}” /> tag is used to display minimum fractional digit.
  • <fmt:formatNumber type=”number” pattern=”###.###$” value=”${balance}” /> is used to format the value in a custom pattern.

Steps for Execution

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server


When the execution process is completed successfully we will get the following output :
jstl_fmt formatNumber Tag_demo

Previous Tutorial : JSTL Format fmt:setBundle Tag :: Next Tutorial : JSTL Format fmt:parseNumber Tag

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!