How to convert a value in an expression Language (EL) double or float to int without rounding?

I use Expression Language (EL) in JSP.

<c:set var="noOfPages" value="${numItems/itemsPerPage}" /> <fmt:formatNumber var="noOfPagesRounded" value="${noOfPages}" maxFractionDigits="0" /> <c:if test="${(numItems % itemsPerPage) > 0}"> <c:set var="noOfPages" value="${noOfPagesRounded + 1}"/> </c:if > 

As you can see, I calculate no. pages required to display x no. results per page.

This does not work all the time, because in line 2 the tag formatNumber rounds the results of my division, which I do not want to round.

i.e. for 73 records of 20 per page you need 4 pages, but I get the result 5, because in line 2 it is rounding the result 3.65 to 4, but I want noOfPagesRounded=3 .

How do I convert a float or double value to int without rounding?

+4
source share
1 answer

If you want noOfPagesRounded to be set to the floor (noOfPages), try this (floor function is not available in EL):

 <fmt:formatNumber var="noOfPagesFloored" value="${noOfPages-(noOfPages%1)}" maxFractionDigits="0" /> 
+7
source

All Articles