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

1

5

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?

Menken answered 6/8, 2012 at 13:6 Comment(0)
E
7

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" />
Extramarital answered 6/8, 2012 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.