惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
博客园 - 司徒正美
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Visual Studio Blog
S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News
博客园 - 聂微东

皓子的小站

网站字体应用之坑——font-family 篇 糟糕的 meta name="theme-color" How to Automatically Track Newly Supported ESLint Rules in Oxlint How to Gradually Migrate from ESLint to Oxlint (Without Breaking Everything) 静态资源预压缩:零运行时开销,极致节省带宽 CVE-2025-70886 Proof of Concept (PoC) | A Script to Crash Halo CMS Comment Backend 自定义网页鼠标指针——一段曲折的旅程 CVE-2025-70886 漏洞复现/PoC | 一个脚本让 Halo CMS 评论后台瘫痪 2025 年终总结 & 博客两周年 老用户专享!已有 Halo 专业版授权可免费升级商业版 自动追踪 Oxlint 对 ESLint 规则的新增支持 Halo 贡献者证书与实体周边盲盒开箱 博客评论系统指南 CDN 回源跟随配置导致登录异常问题排查 SCDN 免费赞助计划:助力高质量博客&博客圈 锐捷校园网:网络共享与带宽叠加方案(哈理工案例) ESLint 到 Oxlint 渐进式迁移快速上手指南 Fixing Vite Breaking Inline JS & CSS in Thymeleaf Templates 解决 Vite 破坏 Thymeleaf 模板内联 JS & CSS 的方法 我的博客,为什么是月更? 博客俱乐部一周年纪念品开箱 网站字体加载之坑——format 篇 经历 1000000000 次 DDoS 请求攻击后,我总结了三条经验 题解分享:[AtCoder Beginner Contest 414 E] Count A%B=C 题解分享:[蓝桥杯 2025 国 Python A] 杨辉三角 P12876 FiF口语训练破解刷分教程(适用于 Windows) 不要与蠢人辩论 小心网络地雷·续篇 当心网络组织“开往”·续篇 当心网络组织“开往”
Random Number Generation and Formatting in Thymeleaf
HowieHz · 2025-02-20 · via 皓子的小站

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
此文章有简体中文版本

Integers

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)}
Click to view examples

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>

Using as List Index

No need for #numbers.formatDecimal when used as list index. Replace my_list with your actual list variable:

Click to view example
<p
  th:with="randomIndex=${T(java.lang.Math).floor(T(java.lang.Math).random()*#lists.size(my_list))}"
  th:text="${my_list[randomIndex]}"
></p>

Formatting Control

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)}
Click to view examples

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>

Decimals/Floats

Generate random float in [min, max)

${T(java.lang.Math).random()*(max-min)+min}
Click to view example

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>

Formatting Control

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)}
Click to view example

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>

Project Status

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