We have a java object:
class Course{
long totalStudens;
long graduatedStudents;
}
And in the Thymeleaf
page we like to show the percentage of graduatedStudents
out of the totalStudens
So we did something like that but we keep on getting 0
in the result, even when the result should not be zero:
<div th:with="result=${#numbers.formatDecimal((course.graduatedStudents/course.totalStudens)*100, 1, 'POINT', 2, 'COMMA')}">
<td th:text="${result}"></td>
</div>
We also tried something simpler:
<td th:text="${#numbers.formatDecimal(course.graduatedStudents/course.totalStudends, 1, 'POINT', 2, 'COMMA')}"></td>
Is it something to do with the long
and double
issues? And if so, how to solve it? We prefer to do it in the Thymeleaf
side and not in the java
side
Since you are doing long/long
division the result will be long
. If you want to receive floating point precision result you will have to convert one of the factors to double
for example by multiplying it by 1.0
:
<div th:with="result=${#numbers.formatDecimal((1.0 * course.graduatedStudents / course.totalStudens)*100, 1, 'POINT', 2, 'COMMA')}">
<td th:text="${result}"></td>
</div>