我想将我的小数值四舍五入,如下所示:

7,715.625变为7,716

我如何实现这一目标?

有帮助吗?

解决方案

如果您不关心逗号,请 floatformat 将完成这项工作:

{{ value|floatformat:"0" }}

如果你关心逗号,你想要:

{{ value|floatformat:"0"|intcomma }}

(给斯蒂芬提示,指着我 intcomma !)

其他提示

要使用 intcomma 过滤器,您应该将 django.contrib.humanize 添加到 INSTALLED_APPS 设置和 {%load humanize%

中的

你可以使用下面的django,参数“0”来做到这一点。将其舍入以排除小数位,但您可以更改它。

number = 7715.625
rounded_number = round(number,0)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top