Link to home
Create AccountLog in
Avatar of arichexe
arichexe

asked on

jstl fmt number problem

I'm trying to format a number, whereby if there's no decimal place, it displays #.0.  For example, "754" would display as "754.0".  I tried the below to no avail.

<fmt:parseNumber var="num" type="number" pattern="#######0.0#" value="${myNum}"/>
<c:out value="${num}"/>
Avatar of Dushyant Sharma
Dushyant Sharma
Flag of India image

why you have used 0 try the below

<fmt:formatNumber pattern="#####.#"> <c:out value="${num}"/></fmt:formatNumber>
Avatar of Sathish David  Kumar N
Only the integerOnly attribute is unique to <fmt:parseNumber>. This attribute takes a Boolean value indicating whether only the integer portion of the provided value should be parsed. If this attribute's value is true, any digits following the decimal point within the character string being parsed are ignored. The default value for this attribute is false.

http://www.ibm.com/developerworks/java/library/j-jstl0415/
Avatar of arichexe
arichexe

ASKER

Tried both the below, but still doesn't work.

<fmt:parseNumber var="num" type="number" pattern="#######0.0#" integerOnly="false" value="${myNum}"/>
<c:out value="${num}"/>

<fmt:parseNumber var="num" type="number" pattern="########.##" value="${myNum}"/>
<c:out value="${num}"/>
This is the only the way I could make it work. It kind of defeats the purpose of using the tag. The problem is that the tag returns a Number object.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<body>
<c:set var="myNum" value="754" />
<fmt:parseNumber var="num"  pattern="##.0" value="${myNum}"/>
<%
  Number number = (Number)pageContext.getAttribute("num");
%> 
<%=number.floatValue()%>
</body>
</html>

Open in new window

Nice solution, rrz, but I'm not allowed to use non-taglib code in jsps.  Is there another way?
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account