Open In App

JSTL Formatting <fmt:parseNumber> Tag

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

In JSTL, the <fmt:parseNumber> tag is mainly used to parse the numeric string into the numeric format. This allows the developers to properly and efficiently convert the string representation of a number into an actual numeric type.

  • This also provides control over the locale-specific formatting.
  • This tag can also be used for handling the user input or for external number values that need to be processed in a standard format.

In this article, we will see the Syntax along with parameters, attributes of this tag, and also the practical implementation.

Syntax of <fmt:parseNumber> Tag

The below code is the syntax for parsing a number in JSTL

<fmt:parseNumber var="resultVar" value="numericString" 
[type="numberType"]
[pattern="formatPattern"]
/>

Where,

  • var: This is the variable to store the parsed numeric result.
  • value: This is the numeric string to be parsed. It can be a specific string or the user input string.
  • type: This is the optional parameter, which is a type of number to parse. Types are numbers or currency.
  • pattern: This is the optional parameter, which is the format pattern for parsing. This is used in custom formats.

Attributes of <fmt:parseNumber> Tag

The table below describes the attributes present in <fmt:parseNumber> with proper description.

Attribute

Description

Required

Default

value

This is the numeric string which is to be parsed

Yes

N/A

type

This is the type of number to parse. Can be a number or currency

No

number

parseLocale

The locale to be used for parsing

No

Default Locale

interferon

This attribute indicates whether only integer values should be parsed

No

false

pattern

This is the format pattern for parsing the custom formats.

No

N/A

timeZone

time zone to be used for parsing the provided time

No

Default time zone

var

The variable in which the parsed result will be stored

Yes

N/A

scope

This is the scope of variables like page, request, session, and application.

No

page

Example JSTL Formatting <fmt:parseNumber> Tag

In this example, we will discuss the example for parseNumber using <fmt:parseNumber>

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:parseNumber Example</title>
  </head>
  <body>
    <c:set var="numericString" value="1,234.56"/>
    <h3>Original Number (String Format): ${numericString}</h3>
      
    <fmt:parseNumber var="parsedNumber"
                     value="${numericString}"
                     type="currency"
                     parseLocale="en_US"
                     integerOnly="true"
                     pattern="#,###.##"
                     scope="request"/>
      
    <h3>Parsed Number (Integer Only): ${parsedNumber}</h3>
  </body>
</html>


Output:

Original Number (String Format): 1,234.56
Parsed Number (Integer Only): 1234

Output of the JSTL <fmt:parseNumber> Tag Program:

Output of the Program

Explanation of the above Program:

  • Firstly, have set the numericString variable with the value as “1234.56“. This is the string format value that will be parsed.
  • Then by using the <fmt:parseNumber> tag, we have parsed the numeric string with various attributes.
  • The parsed number is then stored in the parsed number variable win the request scope.
  • Also, we have set the attribute intergerOnly = true. This is the return of the absolute number without any decimals.
  • We are lastly, showing the output using ${parsedNumber} an <h3> tag of HTML.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads