16

In JSTL,

<fmt:formatNumber value="${1.6}" type="number" pattern="#"/>

returns 2 and the following

<fmt:formatNumber value="${1.4}" type="number" pattern="#"/>

returns1 and I need 2, a ceiling of a number.

Is there a direct way to achieve this in JSTL (or the only way to do so is by using an appropriate custom tag)?

Tiny
  • 27,221
  • 105
  • 339
  • 599

2 Answers2

21

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="#" />
Parker
  • 7,244
  • 12
  • 70
  • 92
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
5

Try this code:

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

where N is the name of your variable.

Regards

esmoreno
  • 658
  • 5
  • 12