45

I am stuck with formatting the currency in HTML 5. I have application where I have to format the currency. I have below code snippet

 <td class="right"><span th:inline="text">$ [[${abc.value}]]</span></td>

Where from DAO abc I am reading the currency value, it should be formatted. Currently printing $ 1200000.0 it should print $ 1,200,000.0 .0

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
giri
  • 26,773
  • 63
  • 143
  • 176

4 Answers4

78

You can use the #numbers utility object, which methods you can see here: http://www.thymeleaf.org/apidocs/thymeleaf/2.0.15/org/thymeleaf/expression/Numbers.html

For example:

<span th:inline="text">$ [[${#numbers.formatDecimal(abc.value, 0, 'COMMA', 2, 'POINT')}]]</span>

Nevertheless, you can also do this without inlining (which is the thymeleaf recommended way):

<td>$ <span th:text="${#numbers.formatDecimal(abc.value, 0, 'COMMA', 2, 'POINT')}">10.00</span></td>
Daniel Fernández
  • 7,335
  • 2
  • 30
  • 33
  • 4
    Is there any possibility to remove the zero's `10.00`, hmm It will be more clean to only show the decimals when there are any, but if we have a decimal number so it get rounded to `435.89`?? – azerafati Jan 21 '15 at 12:23
  • I had the same question. See: https://stackoverflow.com/questions/52066661/remove-trailing-zeros-on-currency – riddle_me_this Dec 26 '18 at 00:01
28

I recommend to use the DEFAULT value (= based on locale) in case your application has to deal with different languages :

${#numbers.formatDecimal(abc.value, 1, 'DEFAULT', 2, 'DEFAULT')}

From Thymeleaf doc (more precisely NumberPointType) :

/* 
 * Set minimum integer digits and thousands separator: 
 * 'POINT', 'COMMA', 'NONE' or 'DEFAULT' (by locale).
 * Also works with arrays, lists or sets
 */
${#numbers.formatInteger(num,3,'POINT')}
${#numbers.arrayFormatInteger(numArray,3,'POINT')}
${#numbers.listFormatInteger(numList,3,'POINT')}
${#numbers.setFormatInteger(numSet,3,'POINT')}

/*
 * Set minimum integer digits and (exact) decimal digits, and also decimal separator.
 * Also works with arrays, lists or sets
 */
${#numbers.formatDecimal(num,3,2,'COMMA')}
${#numbers.arrayFormatDecimal(numArray,3,2,'COMMA')}
${#numbers.listFormatDecimal(numList,3,2,'COMMA')}
${#numbers.setFormatDecimal(numSet,3,2,'COMMA')}
user1075613
  • 725
  • 9
  • 13
  • 5
    If value is below `1`, e.g. `0.55`, then result of this expression is `.55`. I recommend setting minimum integer digits to `1`: `${#numbers.formatDecimal(abc.value, 1, 'DEFAULT', 2, 'DEFAULT')}`. Then result is `0.55`. – naXa stands with Ukraine Feb 03 '19 at 17:38
13

You can now more simply call the formatCurrency method in the numbers utility:

#numbers.formatCurrency(abc.value)

This will remove the need for a currency symbol as well.

Example: <span th:remove="tag" th:text="${#numbers.formatCurrency(abc.value)}">$100</span>

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
  • This method is nowhere to be found in Thymeleaf's API. – Martín Straus Mar 24 '18 at 15:33
  • https://www.thymeleaf.org/apidocs/thymeleaf/3.0.9.RELEASE/org/thymeleaf/expression/Numbers.html#formatCurrency-java.lang.Number- And you can click the Upvote to undo your vote. – riddle_me_this Jun 29 '18 at 01:44
  • what if I need to format a different currency? Euro, pound, etc. – naXa stands with Ukraine Dec 29 '18 at 01:43
  • 1
    That would be outside the scope of this question, of course, since OP is looking purely at showing dollars (current locale) and there is no requirement for multiple currencies. But there is an open issue on GitHub for exactly this: https://github.com/thymeleaf/thymeleaf/issues/604. To describe the external link, the longer form using `formatDecimal` would be used, but there could be some interest in a `formatCurrency(number, locale)` method. – riddle_me_this Dec 29 '18 at 03:03
0

You'd inline using Thymeleaf's numbers utility object as follows:

<span>[[${#numbers.formatCurrency(abc.value)}]]</span>

In the view, it'll even prepend the dollar sign ($) for you.

Isaac Riley
  • 290
  • 4
  • 5
  • You'll lose the default text between the span tags with inlining. Open the page directly in a browser (without the container/server) and you'll see the difference. This is a major strength of Thymeleaf that you'd be losing. – riddle_me_this Dec 25 '18 at 23:58
  • Agreed. The original post used inlining and my suggestion was biased that-a-ways. – Isaac Riley Dec 26 '18 at 04:00