Removing the currency sign in formatCurrency() in thymeleaf

42 Views Asked by At

With this

 th:text="*{#numbers.formatCurrency(roomEntityList.get(0).price)}"

my browser will show 235.235 đ in my country. I want it to show 235.235 instead.

How can i remove the sign 'đ'? I have already look it up but it seem theres not much we can do with formatCurrency in html

1

There are 1 best solutions below

0
Aaron Meese On

I don't see any native support for this in the documentation, but one way you can accomplish it is with regular expressions:

<span
  th:with="originalPrice=${#numbers.formatCurrency(roomEntityList.get(0).price)}, 
           onlyNumbersAndDot=${#strings.replaceAll(originalPrice, '[^\\d.]+', '')}" 
  th:utext="${onlyNumbersAndDot}"
></span>

First, th:with is used to create two variables:

  • originalPrice holds the formatted currency value
  • onlyNumbersAndDot applies the regular expression [^\\d.]+ to originalPrice, which matches any unwanted characters so replaceAll can replace them with an empty string

Then th:utext is then used to display the modified price, which now contains only numbers and dots.