Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I round a number in JSTL?

I'm doing a division in a JSP and I'd like to round the result - how should I do this?

i.e.

<c:set
  var="expiry"
  value="${(expire.time - now.time) / (60 * 1000)}"/>

...how do I round the result?

Thanks,

like image 452
brabster Avatar asked Sep 18 '09 09:09

brabster


People also ask

How do you round a number in C#?

In C#, Math. Round() is a Math class method which is used to round a value to the nearest integer or to the particular number of fractional digits.

How do you round in TypeScript?

In TypeScript, the floor() method of the Math object is the opposite of the ceil method. It used to round a number downwards to the nearest integer. It returns the largest integer less than or equal to a number.

What is round in coding?

Calculates the closest integer that is closest to the value of the parameter.


2 Answers

As an alternative:

<fmt:formatNumber var="expiry"
  value="${(expire.time - now.time) / (60 * 1000)}"
  maxFractionDigits="0" />

This way you do not lose localization (commas and dots).

like image 194
brabster Avatar answered Oct 17 '22 10:10

brabster


I used:

${fn:substringBefore(expiry, '.')}

which truncates rather than rounding, but that may be good enough.

like image 10
mm2001 Avatar answered Oct 17 '22 10:10

mm2001