Open In App

JSTL Formatting <fmt:formatNumber> Tag

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In JSTL the <fmt:formatNumber> tag is mainly used to format the numeric value according to the specified localization setting, which also includes the grouping separators, currency symbols, and decimal separators. This function mainly simplified the presentation of numeric data in JSP by offering control over how the numbers should be displayed.

In this article, we will see the detailed syntax along with the parameters of <fmt:formatNumber> Tag. We will see the attributes, and practical implementation in terms of examples.

Syntax:

<fmt:formatNumber value="numericValue" 
[type="numberType"]
[pattern="formatPattern"]
[var="resultVar"]
[scope="page|request|session|application"]
/>

Where,

  • value: This is the numeric value which is to be formatted.
  • type: This is the type of the number to format. The type of numbers is “number“, “currency” or the “percent“.
  • pattern: This is the optional parameter used for formatting in custom formats.
  • var: This is an optional parameter used to store the formatted result.
  • scope: This is the optional parameter that represents the scope of the variable as page, request, session, or application.

Attributes:

The table describes all the attributes present in fmt:formatNumber JSTL tag.

Attribute

Description

Required

Default

value

This is the numeric value which is to be formatted

Yes

N/A

type

This is the type of number to be formatted.

No

number

pattern

This is the format pattern for formatting the custom formats

No

N/A

maxIntegerDigits

This is the maximum number of integer digits

No

N/A

minIntegerDigits

This is the minimum number of integer digits.

No

N/A

maxFractionDigits

This is the maximum number of fraction digits

No

N/A

minFractionDigits

This is the minimum number of fraction digits

No

N/A

groupingUsed

This indicated whether the grouping separators should be used or not

No

true

var

This is the variable used to store the formatted result

No

N/A

scope

This is the scope of the variable

No

page

Example of JSTL Formatting <fmt:formatNumber> Tag

This is a JavaServer Pages (JSP) code that demonstrates the use of JSTL (JavaServer Pages Standard Tag Library) to format a numeric value using the fmt:formatNumber tag.

HTML




<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>JSTL fmt:formatNumber Example</title>
</head>
<body>
<c:set var="numericValue" value="12345.6789"/>
  
<h3>Original Numeric Value: ${numericValue}</h3>
<fmt:formatNumber value="${numericValue}"
                  type="currency"
                  pattern="#,###.##"
                  maxIntegerDigits="5"
                  minIntegerDigits="3"
                  maxFractionDigits="2"
                  minFractionDigits="1"
                  groupingUsed="true"
                  var="formattedNumber"
                  scope="request"/>
<h3>Formatted Number: ${formattedNumber}</h3>
</body>
</html>


Output:

Original Numeric Value: 12345.6789
Formatted Number: 12,345.68

Output of the Program

Explanation of the Above Program:

  • We have initialized the numericValue variable with the value “12345.6789“.
  • Then, the <fmt:formatNumber> tag is used to format the numeric value with different attributes.
  • We are storing the formatted number in the “formattedNumber” variable in the request scope.
  • And lastly, we are displaying both the original and formatted numbers using the <h3> tag in HTML.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads