5

I'm trying to formatting a percentage using Thymeleaf. But, with a % like 99.99, thymeleaf formats this value in 100. And I don't want it.

I done this:

Java side

BigDecimal percentage = (a).multiply(new BigDecimal(100)).divide(b, 3, RoundingMode.HALF_DOWN);

Thymelaf side

th:text="${#numbers.formatDecimal(percentage, 1, 'POINT', 2, 'COMMA')}"

If percentage is 99.99 Thymeleaf gives me 100.00

Why?

Dave
  • 1,428
  • 4
  • 31
  • 49

1 Answers1

0

That happen because your percentage's scale value on Java side is greater than decimalDigits on Thymeleaf side.

Try to set it the same value (to 2):

BigDecimal percentage = (a).multiply(new BigDecimal(100)).divide(b, 2, RoundingMode.HALF_DOWN);

And also as if you are using percentage field only for showing percentages, I think you don't need thousandsPointType parameter ('POINT' in your case), because its value will never be greater than 100.

DimaSan
  • 12,264
  • 11
  • 65
  • 75