Categories: JSTL

JSTL formatNumber Tag <fmt:formatNumber>

JSTL formatNumber tag is used to change the format of numbers or currency.

JSTL formatNumber Example:

To display the numbers on JSP in proper format, you can use the <fmt:formatNumber> tag like this:

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

As you can see above, in the JSTL formatNumber tag, var attribute is used to define the variable than needs to be formatted. Proper formatting needs help of other attributes from formatNumber tag.

Attributes of <fmt:formatNumber> tag are:

      1. var:This attribute provides numeric value to be formatted.

       2. type: This attribute specifies whether the value is to be formatted as number, currency, or percentage.

       3. maxFractionDigits: This attribute provides maximum number of digits in the fractional portion of the formatted output.

       4.  minFractionDigits : This attribute provides Minimum number of digits in the fractional portion of the formatted output.

       5. pattern: This attribute provides custom formatting pattern.

       6. value: This attribute provides Numeric value to be formatted.

      7. scope: This attribute provides scope of var.

       8. currencyCode: This attribute provides ISO 4217 currency code. Applied only when formatting currencies (i.e. if type is equal to “currency”). Ignored otherwise.

      9. currencySymbol: This attribute provides Currency symbol. Applied only when formatting currencies (i.e. if type is equal to “currency”); ignored otherwise.

       10. groupingUsed: This attribute specifies whether the formatted output will contain any grouping separators..

      11. maxIntegerDigits: This attribute provides Maximum number of digits in the integer portion of the formatted output.

       12. minIntegerDigits: This attribute provides Minimum number of digits in the integer portion of the formatted output.

<<Previous <<   || Index ||   >>Next >>

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago