numbersformatthymeleaffractions

How to get the result from fraction without rounding up in Thymeleaf


I am trying to get the relation between the height and width from image and then multiply by a fixed width to resize a picture using Thymeleaf.

For it, I saved the height and width in a java object called g. Using a fixed width: 270px I want to scale the height using the relation between the sized saved doing that:

th:style="'height:' + ${(g.height / g.width) * 270} +';

The saved and original size is: int height = 286 and int width = 423. So the fraction's result should be 286/423 = 0.67. But the problem is that this fraction g.height / g.width is giving me 0 as result.

How can I obtain decimals from this fraction? I tried with {#numbers.formatDecimal()} but not results.


Solution

  • Since both width & height are integers, it's doing integer division. Either store them as float/double on the object itself, or convert them to doubles in the expression.

     th:style="'height:' + ${((0.0 + g.height) / g.width) * 270} +'px;'"