In this post, I will help you understand how to use the <fmt:parseNumber> tag in the JSTL format tags library with code example.

The <fmt:parseNumber> is used to parse the string representation of numbers, currency or percentage.

 

JSTL <fmt:parseNumber> Syntax:

<fmt:parseNumber

  value="<string>"

  type="<string>"

  pattern="<string>"

  parseLocale="<string>"

  integerOnly="<string>"

  var="<string>"

  scope="<string>"/>

 

Attributes:

Name

Required

Type

Description

    

value

False

java.lang.String

String to be parsed.

type

False

java.lang.String

Determines whether the String provided in the value attribute to be parsed as a NUMBER, CURRENRY or PERCENTAGE. Default is NUMBER.

pattern

False

java.lang.String

Pattern on how the given string in the value attribute is parsed.

parseLocale

False

java.lang.String

Locale to use when parsing the value   using the given pattern.

integerOnly

False

java.lang.String

Specifies whether only the integer part of the value is parsed.

var

False

java.lang.String

Name of the variable which stores the result of the parsed value. The result is of type java.lang.Number.

scope

False

java.lang.String

Scope for the var to store.

 

JSTL <fmt:parseNumber> Example:

The following JSP code takes account balance and parses it as a number. Note that we are displaying the number with and without its fractional value.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ 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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>&lt;fmt:parseNumber&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;fmt:parseNumber&gt; Demo</h1>
    <c:set var="accountBalance" value="9552.75" />
    <fmt:parseNumber var="parsedNumber" type="number" value="${accountBalance}" />
    <p>Account Balance: <c:out value="${parsedNumber}" /></p>
    <fmt:parseNumber var="parsedNumber" integerOnly="true" type="number" value="${accountBalance}" />
    <p>Account Balance (without cents): <c:out value="${parsedNumber}" /></p>
  </body>
</html>

 

Output:

parsenumber

 

Recommended Usage of JSTL <fmt:parseNumber> tag:

The <fmt:parseNumber> tag is used to parse the string representation of numbers, currency and percentage values.

 

Other JSTL Format Tags:

bundle  |  formatDate  |  formatNumber  |  message  |  param  |  parseDate  |  requestEncoding  |  setBundle  |  setLocale  |  setTimeZone  |  timeZone


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.

Add comment