You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo报表:如何在float_time组件中显示带千分位逗号的数值

问题解决:同时实现float_time展示与千分位格式化

问题背景

我有一个变量ot10_total,值为1758.92:

  • 使用代码<span t-esc="'{0:,.2f}'.format(ot10_total)"/>可给数值添加千分位逗号,显示为1,758.92
  • 使用代码<span t-esc="ot10_total" t-options="{'widget': 'float_time'}"/>可通过float_time组件将数值转为时分格式,显示为1758:55

尝试同时实现两个功能时,使用代码<span t-esc="'{0:,.2f}'.format(ot10_total)" t-options="{'widget': 'float_time'}"/>触发错误:TypeError: bad operand type for abs(): 'str'

解决方法

错误根源是你先把数值格式化为了字符串,而float_time组件仅支持数值类型输入,无法处理字符串。正确思路是先通过float_time的逻辑将数值转为时分格式,再对小时部分添加千分位。

方案1:Python层预处理(推荐)

在模型或视图代码中提前处理好格式化逻辑:

# 拆分小时与分钟
hours = int(ot10_total)
minutes = int(round((ot10_total - hours) * 60))
# 给小时部分添加千分位
formatted_hours = '{0:,}'.format(hours)
# 拼接成最终时分字符串
formatted_time = f"{formatted_hours}:{minutes:02d}"

然后在模板中直接渲染处理后的结果:

<span t-esc="formatted_time"/>

方案2:模板内直接处理

若不想修改Python代码,可在模板中用表达式分步计算:

<span t-esc="f'{int(ot10_total):,}:{int(round((ot10_total - int(ot10_total))*60)):02d}'"/>

两种方案最终都会输出带千分位的时分格式:1,758:55

内容的提问来源于stack exchange,提问作者Jubb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 19:16:07