Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format number in jsp

How do I format an int value of 123456789 as 123,456,789?

like image 252
mysticfalls Avatar asked Apr 01 '11 00:04

mysticfalls


2 Answers

Use JSTL fmt:formatNumber

http://download.oracle.com/docs/cd/E17802_01/products/products/jsp/jstl/1.1/docs/tlddocs/fmt/formatNumber.html

Set your pattern to #,##0

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber pattern="#,##0" value="${value}" />

This will require you having The JSTL Standard tag library in your WEB-INF/lib folder

http://tomcat.apache.org/taglibs/standard/

Now I'm not 100% sure, but most modern containers provide the "core" API library jstl.jar and your web app must provide the implemenation. In the case of the above link that should be standard.jar included with the download.

like image 141
Dave G Avatar answered Sep 20 '22 13:09

Dave G


try this code

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    DecimalFormat formatter = new DecimalFormat("###,###,###");
    System.out.print(formatter.format(123456789));
}

}

you can write a function in jsp block <%! %> and the code inside the main method.

like image 24
Nathanphan Avatar answered Sep 20 '22 13:09

Nathanphan