Ceiling of a number in JSTL/EL

前端 未结 2 1451
庸人自扰
庸人自扰 2020-12-09 10:14

In JSTL,


returns 2 and the following



        
相关标签:
2条回答
  • 2020-12-09 10:39

    Try this code:

    <fmt:formatNumber value="${N+(1-(N%1))%1}" type="number" pattern="#"/>
    

    where N is the name of your variable.

    Regards

    0 讨论(0)
  • 2020-12-09 10:54

    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="#" />
    
    0 讨论(0)
提交回复
热议问题