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.

AttributesDescription
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.

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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>formatNumber Tag</title>
</head>
<body>

	<h3>Number Format:</h3>

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

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

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

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

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

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

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

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

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

</body>
</html>

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

Output:

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

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*