Search code examples
javajspjstljsp-tags

JSTL - formatNumber : how to show phone number in (###) ####### format


I am trying to format phone number in (###) ####### format. I am doing like that ..

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<fmt:formatNumber type="number" pattern="(###) #######" value="${phoneNumber}" />

but it is not converting is the correct format. Showing value like that ..

Output

(9000000000)

It should show me like that .. (900) 0000000

I am putting normal 10 digit phone number. It should show me in proper format in front end. Help me


Solution

  • Let's try it using fn:substring and fn:length.

    <c:set value="9123456789" var="phone"/>
    <c:out value="(${fn:substring(phone, 0, 4)}) ${fn:substring(phone, 4, fn:length(phone))}"/>
    

    output:

    (9123) 456789