JSTL/EL中的数字上限 [英] Ceiling of a number in JSTL/EL

查看:76
本文介绍了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)?

推荐答案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆