原谅我今晚的脑筋急转弯,但出于某种原因...这是我现在能想到的最好的解决方案,用JSTL获得BigDecimal的ABS……除了ABS之外,也没有数学技巧。我必须保持精确性。
我知道有更好的方法来处理它。你的建议呢?任何谷歌搜索都会在formatNumber上拉出帮助,并处理增量/负片的货币。
<c:forEach items="${arr}" var="cursor" varStatus="itemsRow">
<c:choose>
<c:when test="${cursor.value < 0}">
<td width="75px" align="right">
<fmt_rt:formatNumber pattern="#,###,###,###.##" value="${cursor.value * -1}" minFractionDigits="2"/></td>
</c:when>
<c:otherwise>
<td width="75px" align="right">
<fmt_rt:formatNumber pattern="#,###,###,###.##" value="${cursor.value}" minFractionDigits="2"/></td>
</c:otherwise>
</c:choose>
</c:forEach>发布于 2011-12-16 12:58:42
在服务器端使用BigDecimal.abs();不要在JSP中执行这类工作。
如果需要,可以将其包装在基于JSP的自定义标记中,或者创建一个JSTL函数包装器来处理abs。
另外,重构,如果你不能在适当的地方做工作,就像这样(完全未经测试):
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${arr}" var="cursor" varStatus="itemsRow">
<c:set name="val" value="${cursor.value < 0 ? cursor.value * -1 : cursor.value}"/>
<td width="75px" align="right">
<fmt_rt:formatNumber pattern="#,###,###,###.##" value="${val}" minFractionDigits="2"/>
</td>
</c:forEach>发布于 2011-12-16 12:55:50
假设您正在使用某种MVC框架,或者至少有一个servlet将数据提供给您的JSP,我建议您在控制器/ servlet中将BigDecimals列表构建为绝对值-使用BigDecimal.abs() -这样您就不必用这种东西搞乱BigDecimals。
https://stackoverflow.com/questions/8529867
复制相似问题