如何在我的 Django/Python 模板中显示数字的四舍五入版本?

How do I display a rounded version of a number in my Django/Python template?

我正在使用 Django 和 Python 3.7。在我的模板中我有这个

{{ round(articlestat.votes) }}

导致错误

Could not parse the remainder: '(articlestat.votes)' from 'round(articlestat.votes)'

有没有办法在不在模型中创建额外的@属性 方法的情况下显示我的数字的四舍五入版本?每次我希望数据格式略有不同时都执行一种方法似乎有点过分。

我建议您使用 floatformat 过滤器,例如:

{{ articlestat.votes|floatformat:"0" }}