• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JSTL Format fmt:formatNumber Tag

February 24, 2014 //  by Krishna Srinivasan//  Leave a Comment

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.

Attributes Description
value Specifies the numeric value that has to be formatted.
type Specifies that the value has to be formatted as number, currency or percentage.
pattern Specifies the custom pattern to format the given value.
var Specifies the variable name to which the value is to be formatted.
scope The Scope into which the variable number has to be formatted.
currencyCode Specifies the code of currency.
currencySymbol Specifies the symbol of currency.
groupingUsed Specifies that whether the output value is to be grouped by using comma or seperator in the output.
maxIntegerDigits Specifies the maximum number of digits.
minIntegerDigits Specifies the minimum number of digits.
minFractionDigits Specifies 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

Category: Java EETag: JSTL Tutorials

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.

Previous Post: « EclipseLink / JPA Annotations – @OneToMany and @ManyToOne
Next Post: JSTL Format fmt:parseNumber Tag »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact