Open In App

JSTL Formatting <fmt:parseNumber> Tag

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.

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,



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>




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

Explanation of the above Program:


Article Tags :