alainlompo
alainlompo

Reputation: 4434

Thymeleaf #numbers.formatInteger strange behaviour

I am developping a user story for a web application using spring mvc and thymeleaf as the templating engine.

My controller passes to my view an object that contains a BigDecimal field called quantite. Even though it is a BigDecimal I am required to render it as an Integer for It can never have a fractional component, so my goal is to render a BigDecimal like 50.00 into 50 I render it in the view with Thymeleaf using the following syntax.

<span th:text="${#numbers.formatInteger(placAction.quantite,2,'POINT')}"></span> 

which means that I want to format the number as an integer to display a minimum of two digits and to use the dot as thousands separator.

The following image shows the result I am having

format not applied

So the format is not correctly applied. As I was testing this behaviour I noticed that adding a litteral to the expression provokes the correct rendering of the number. For example I changed the previous expression this way:

<span th:text="${#numbers.formatInteger(placAction.quantite,2,'POINT')} + ' (no units)'"></span>

And here is an image illustrating the rendering

format correctly applied

As you can see now, the format is correctly rendered. I don't understand this strange behaviour, it seems as though Thymeleaf does not apply the #numbers.formatInteger until I sum its result with a litteral, and only when the litteral is not blank (made of whitespaces only) for I have also tested

<span th:text="${#numbers.formatInteger(placAction.quantite,2,'POINT')} + ' '"></span>

But it did not produce the expected result. Every change that adds a non - blank litteral to the expression renders it correctly, but then I have an undesired additional text, but when I remove it, the rendering is incorrect again. So any help to solve this issue is very welcome.

Upvotes: 2

Views: 3114

Answers (1)

Wilson Campusano
Wilson Campusano

Reputation: 616

if you are using thymeleaf 2.0 version try this instead

<span th:text="|${#numbers.formatInteger(placAction.quantite,2,'NONE')}|"></span>

Upvotes: 2

Related Questions