简体   繁体   中英

issue with format tag of JSTL in JSP Page

I have following code in my JSP Page-

<fmt:formatNumber value="${(r.p51_vmeset-row.p51_vmeset)}" maxFractionDigits="2" minIntegerDigits="2"  var="mm" />
<td style="${mm eq 0 ? 'background-color: lime':'background-color:  pink'}">
<c:out value="${mm}" ></c:out></td>

Through this code I want to display that whatever the result of subtraction if the fractional part display zero in the first two digits and zero in integer part ,then the result must be stored in variable mm and lime color should be displayed as maxFractionDigits="2" minIntegerDigits="2" is equal to zero of the subtraction.

But what is displayed is that,if the result is zero then only lime is displayed.But I want that if integer part and fractional part upto two digits is zero then also lime color should be display as background of column.

Example -

Suppose my r.p51_vmeset value is -0.4000977 and row.p51_vmeset value is -0.40009767 .Then the subtraction of both values will give -0.0000003 but I want this to be trimmed as 00.00 and the color should be lime not pink.

I tried printing my result as-

 <fmt:formatNumber value="${(r.p99_vmeset-row.p99_vmeset)}" maxFractionDigits="2" minIntegerDigits="2"  var="mm" />
     **<c:out value="${mm}"></c:out>**
     <td style="${mm eq 0 ? 'background-color: lime':'background-color: pink'}" >
      <c:out value="${mm}" ></c:out></td>

The c:out shows value 00.00 But when i compare it in that if mm is equal to zero background color should be lime but its showing pink.Y??

You can use following to format your number to integer:

<fmt:formatNumber var="i" maxIntegerDigits="3" maxFractionDigits="0"
                   type="number" value="${mm}" />

and then compare here:

<td style="${i eq 0 ? 'background-color: lime':'background-color:  pink'}">

If you want to have format ##.## for whatever value of subtraction, try this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

//use pattern="00.00"(0 represents a digit), instead of max/min
<fmt:formatNumber value="${(r.p51_vmeset-row.p51_vmeset)}" pattern="00.00"  var="mm" />  

//remove "-" in case it's negative value
<c:set var="mm2" value="${fn:replace(mm, '-', '')}" />

//compare it with string '00.00', not number
<td style="${(mm2 eq '00.00') ? 'background-color: lime':'background-color:  pink'}">
<c:out value="${mm2}" ></c:out></td>

This should work, I just tested. Vote me up if works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM