JSTL/EL 中数字的上限 [英] Ceiling of a number in JSTL/EL
本文介绍了JSTL/EL 中数字的上限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 JSTL 中,
<fmt:formatNumber value="${1.6}" type="number" pattern="#"/>
返回 2
和以下
<fmt:formatNumber value="${1.4}" type="number" pattern="#"/>
返回1
,我需要2
,一个数字的上限.
在 JSTL 中是否有直接的方法来实现这一点(或者唯一的方法是使用适当的自定义标记)?
Is there a direct way to achieve this in JSTL (or the only way to do so is by using an appropriate custom tag)?
推荐答案
DecimalFormat
,由
是 RoundingMode.HALF_EVEN
.没有办法通过任何标签属性来改变它.当它不是一个奇数时,只需将 0.5
添加到值中,使其表现得像 RoundingMode.CEILING
.
The default rounding mode of DecimalFormat
that is used by <fmt:formatNumber>
is RoundingMode.HALF_EVEN
. There is no way to change that via any tag attribute. Just add 0.5
to the value when it's not an odd integer to make it to behave like RoundingMode.CEILING
.
<fmt:formatNumber value="${bean.number + (bean.number % 1 == 0 ? 0 : 0.5)}"
type="number" pattern="#" />
这篇关于JSTL/EL 中数字的上限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文