In JSP, the easiest is to use the formatNumber tag of the JSTL I18N library. For 
example, the following JSP page:

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" 
%>
<fmt:formatNumber value="0.33333" maxFractionDigits="2"/>
<br/>
<fmt:formatNumber value="0.33533" maxFractionDigits="2"/>
<br/>
<fmt:formatNumber value="0.3" minFractionDigits="2" 
maxFractionDigits="2"/>
<br/>
<fmt:formatNumber value="${10/3}" minFractionDigits="2" 
maxFractionDigits="2"/>

gives you:

0.33 
0.34 
0.30 
3.33 

Don't forget to copy the jstl.jar and standard.jar to your WEB-INF/lib. Also, if you 
are using Tomcat 4, you can't enjoy the EL expression in the last formatNumber.

Hope this helps,
budi


   >  Lee Chin Khiong  wrote:
   >  How to format a double in JSP or java ?
   >  
   >  for example :
   >  
   >  10/3=3.3333333333333
   >  
   >  I want to format it to 3.33.
   >  



Author of
- How Tomcat Works (sample chapters downloadable from www.brainysoftware.com) 
- JavaServer Faces Programming (McGraw-Hill Osborne Media)
- Java for the Web with Servlets, JSP, and EJB (NewRiders)
- Java Web Development with Servlets, JSP, and EJB, Second Edition (Sam Publishing)

Reply via email to