1. Introduction
Thymeleaf is a template engine created for Java-based applications. The engine provides many utility methods that can be used to format or transform variables directly on the template level. In this article, we will present utility methods for Number
objects. The implementation of those utility methods can be found in the official Thymeleaf GitHub Repository.
2. Available utility methods for Numbers
Thymeleaf comes with several utility methods to format Number
objects.
Method | Purpose | Description |
---|---|---|
${#numbers.formatInteger(num,3)} ${#numbers.arrayFormatInteger(numArray,3)} ${#numbers.listFormatInteger(numList,3)} ${#numbers.setFormatInteger(numSet,3)}
|
Set minimum integer digits | 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 thousands separator | Available separators: 'POINT', 'COMMA', 'WHITESPACE', 'NONE' or 'DEFAULT' (by locale). |
${#numbers.formatDecimal(num,3,2)} ${#numbers.arrayFormatDecimal(numArray,3,2)} ${#numbers.listFormatDecimal(numList,3,2)} ${#numbers.setFormatDecimal(numSet,3,2)}
|
Set minimum integer digits and (exact) decimal digits | 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')}
|
Set minimum integer digits and (exact) decimal digits, and also decimal separator. | Available separators: 'POINT', 'COMMA', 'WHITESPACE', 'NONE' or 'DEFAULT' (by locale). |
${#numbers.formatDecimal(num,3,'POINT',2,'COMMA')} ${#numbers.arrayFormatDecimal(numArray,3,'POINT',2,'COMMA')} ${#numbers.listFormatDecimal(numList,3,'POINT',2,'COMMA')} ${#numbers.setFormatDecimal(numSet,3,'POINT',2,'COMMA')}
|
Set minimum integer digits and (exact) decimal digits, and also thousands and decimal separator. | |
${#numbers.formatCurrency(num)} ${#numbers.arrayFormatCurrency(numArray)} ${#numbers.listFormatCurrency(numList)} ${#numbers.setFormatCurrency(numSet)}
|
Formatting currencies | |
${#numbers.formatPercent(num)} ${#numbers.arrayFormatPercent(numArray)} ${#numbers.listFormatPercent(numList)} ${#numbers.setFormatPercent(numSet)}
|
Formatting percentages | |
${#numbers.formatPercent(num, 3, 2)} ${#numbers.arrayFormatPercent(numArray, 3, 2)} ${#numbers.listFormatPercent(numList, 3, 2)} ${#numbers.setFormatPercent(numSet, 3, 2)}
|
Set minimum integer digits and (exact) decimal digits. | |
${#numbers.sequence(from,to)} ${#numbers.sequence(from,to,step)}
|
Sequence from x to y | Create a sequence (array) of integer numbers going from x to y |
3. Conclusion
In this article, we presented Thymeleaf utility methods for Number
objects. Methods can be useful for formatting numbers, currencies or percentages. With this utility, you do not need to prepare variables before putting them on the template but instead, you can format them accordingly to your needs, directly on the template.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}