简体   繁体   English

JSP页面中的JSTL格式标记的问题

[英]issue with format tag of JSTL in JSP Page

I have following code in my JSP Page- 我的JSP页面中有以下代码

<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. 通过此代码,我想显示的是,如果小数部分的前两位数字为零,整数部分为零,则无论减法结果如何,然后结果必须存储在变量mm中,并且石灰颜色应显示为maxFractionDigits =“ 2 “ minIntegerDigits =“ 2”等于减法的零。

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. 假设我的r.p51_vmeset值为-0.4000977且row.p51_vmeset值为-0.40009767。然后将这两个值相减将得到-0.0000003,但我希望将此值修剪为00.00,并且颜色应为石灰而不是粉红色。

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?? c:out显示值为00.00,但是当我比较它时,如果mm等于零,则背景色应为石灰,但其显示为粉红色。

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. 如果可行,请投票给我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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