Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ceiling of a number in JSTL/EL

Tags:

jsp

jstl

el

ceil

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)?

like image 327
Tiny Avatar asked Jul 31 '12 07:07

Tiny


People also ask

What is JSTL El?

A primary feature of JSTL is its support for an expression language (EL). An expression language, in concert with JSTL tags, makes it possible to easily access application data and manipulate it in simple ways without having to use scriptlets or request-time expressions.

How do I find the length of an array in JSTL?

JSTL - fn:length() Function The fn:length() function returns the string length or the number of items in a collection.

Which tag is used in JSTL to show the output?

The <c:out> tag is similar to JSP expression tag, but it can only be used with expression. It will display the result of an expression, similar to the way < %=... % > work.

What is C set in JSTL?

JSTL Core Tags c:set allows to set the result of an expression in a variable within a given scope. Using this tag helps to set the property of 'JavaBean' and the values of the 'java. util. Map' object.


1 Answers

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="#" />
like image 97
BalusC Avatar answered Oct 02 '22 11:10

BalusC