Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert in Expression Language (EL) double or float value to int without round-off?

I am using 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 see I am calculating no. of pages required to display x no. of results per page.

This doesn't work all time since at line 2 i.e. formatNumber tag is rounding of my division results, which I don't want to get rounded off.

i.e. for 73 records 20 per page needs 4 pages but I am getting result 5, because at line 2 it is rounding of result 3.65 to 4 but I want noOfPagesRounded=3.

How should I convert float or double value in int without rounding off?

like image 604
user1073214 Avatar asked Aug 06 '12 13:08

user1073214


1 Answers

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

<fmt:formatNumber var="noOfPagesFloored" value="${noOfPages-(noOfPages%1)}"  maxFractionDigits="0" />
like image 153
Martin Wilson Avatar answered Nov 19 '22 23:11

Martin Wilson