

























All examples presented in this article have been tested on Thymeleaf 3.1 and are applicable to various Thymeleaf scenarios, such as generating random numbers in Halo CMS themes.
This article is also available in: Simplified Chinese
此文章有简体中文版本
Generate random integer in range [min, min+range)
${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),0,0)}
Generate random integer in range [min, max]
${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),0,0)}
A <p> tag displaying an integer between [1, 6) ([1, 5]). Possible results: 1, 2, 3, 4, 5.
Method 1
<p
th:with="range=5,min=1"
th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),0,0)}"
></p>
Method 2
<p
th:with="max=5,min=1"
th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),0,0)}"
></p>
No need for #numbers.formatDecimal when used as list index. Replace my_list with your actual list variable:
<p
th:with="randomIndex=${T(java.lang.Math).floor(T(java.lang.Math).random()*#lists.size(my_list))}"
th:text="${my_list[randomIndex]}"
></p>
Formatted random integer in [min, min+range)
Format: Integer part ≥ integerDigits (padded with leading zeros), decimal part fixed to decimalDigits.
${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),integerDigits,decimalDigits)}
Formatted random integer in [min, max]
Same formatting rules as above:
${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),integerDigits,decimalDigits)}
A <p> tag displaying an integer between [0, 5) ([0, 4]). Possible results: 000.00, 001.00, 002.00, etc.
Method 1
<p
th:with="range=5,min=0,integerDigits=3,decimalDigits=2"
th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),integerDigits,decimalDigits)}"
></p>
Method 2
<p
th:with="max=4,min=0,integerDigits=3,decimalDigits=2"
th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),integerDigits,decimalDigits)}"
></p>
Generate random float in [min, max)
${T(java.lang.Math).random()*(max-min)+min}
A <p> tag displaying a float between [0.5, 1.8). Possible results: 1.46833041859471749, 0.5578399752996518908, etc.
<p
th:with="max=1.8,min=0.5"
th:text="${T(java.lang.Math).random()*(max-min)+min}"
></p>
Formatted random float in [min, max)
Format: Integer part ≥ integerDigits (padded with leading zeros), decimal part fixed to decimalDigits.
${#numbers.formatDecimal(T(java.lang.Math).random()*(max-min)+min,integerDigits,decimalDigits)}
A <p> tag displaying a float between [0.5, 150.8). Possible results: 00.83, 70.22, 150.23, etc.
<p
th:with="max=150.8,min=0.5,integerDigits=2,decimalDigits=2"
th:text="${#numbers.formatDecimal(T(java.lang.Math).random()*(max-min)+min,integerDigits,decimalDigits)}"
></p>
As of publication (February 20, 2025), the Thymeleaf random number feature request remains open with no updates:
Random integer in #numbers · Issue #787 · thymeleaf/thymeleaf
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。