While working with numbers and dates and times, we need to modify it in multiple formats as per the requirement. In java or any programming language, we can achieve this easily but Also at the frontend end side in JSP and HTML. We can achieve this using JSTL.
In this JSTL tutorial, We will see some hands-on examples for number formatting and DateTime formatting in JSTL.
Number Formating in JSTL
Define a long decimal number, Print it, and Add some number formatting check Like Display only 3 Digit before decimal or Display only 2 digits after the decimal.
To perform this operation, JSTL provides tags from Format taglib that is <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
Define a number as a variable in JSTL
<c:set/> tag is used to declare a variable in JSTL that contains var=”number” properties to define the name of variable and value=”152485255.021545787″ attribute to define the value of variable.
<c:set var="number" value="152485255.021545787"/>
The print number is JSTL
<c:out/> tag is used to print or display the value of a declared variable with the help of attribute value=”${someNumber}”. and the value is using expression ${} format to print it.
<c:out value="${someNumber}" />
Display only 3 digits before the decimal in JSTL
maxIntegerDigits=”3″ attribute allow to set the max integer digit that is used to under format <fmt:formatNumber /> tag of JSTL
<fmt:formatNumber type="number" maxIntegerDigits="3" value="${someNumber}" />
Set max 2 digits after the decimal in JSTL
maxFractionDigits=”2″ attribute allow to set the max Fractions digit that is used to under format <fmt:formatNumber /> tag of JSTL
<fmt:formatNumber type="number" maxFractionDigits="2" value="${someNumber}" />
Number Formating Example in JSTL
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Number formating Demo</title> </head> <body> <c:set var="number" value="152485255.021545787"/> <b><c:out value="${someNumber}" /></b> <br/> <br/> Max Integer Digits : <fmt:formatNumber type="number" maxIntegerDigits="3" value="${number}" /> <br/> Max Fraction Digits :<fmt:formatNumber type="number" maxFractionDigits="2" value="${number}" /> <br/> </body> </html>
Output:
Currency formating in JSTL
Display currency as per the Country(locale_Id). Yes, we can show the different country currency symbol by using JSTL format tag lib.
Here, will continue the above example and display the result in multiple currencies. To convert number into currency use type=”currency” attribute inside the <fmt:formatNumber/> tag.
<fmt:setLocale value=”en_UK”/> is used to set the locale_Id as per the country like for UK:en_UK or US: en_US Checkout more local ID
Currency : <fmt:formatNumber value="${number}" type="currency"/> <br/> <fmt:setLocale value="en_UK"/> Currency : <fmt:formatNumber value="${number}" type="currency"/> <fmt:setLocale value="en_US"/> Currency : <fmt:formatNumber value="${number}" type="currency"/>