Round, Floor and Ceiling in Expression Language

Expression Language makes working with Java Server Pages a slightly less pain in the behind. But, its functionality is very limited, there is for instance no way to round off a number, neither as floor, ceiling or to the closest integer


?View Code JAVA
Floor(foo) -> ${foo - ( foo % 1 ) }
Ceiling(foo) -> ${foo + ( 1 - (foo%1)) % 1}
Round(foo) -> ${foo +0.5 - ((foo+0.5) % 1) }

This still gives you a float, so to get an integer, you must either use the <fmt:formatNumber>-tag

<fmt:formatNumber value="${foo - ( foo % 1 ) }" var="fooAsInt" maxFractionDigits="0"/>

(really, this returns a string, representing the number without the decimals. But as EL automatically casts between strings, floats and integers, this is mostly OK.)

or you can hack it using fn:replace():

?View Code JAVA
${fn:replace(foo - ( foo % 1 ),'.0','') }

(Yes, you may use singlequotes for strings in EL, even though this is supposed to be Java….)