gpt4 book ai didi

python - 在 django 模板中格式化数字

转载 作者:IT老高 更新时间:2023-10-28 12:29:28 26 4
gpt4 key购买 nike

我正在尝试格式化数字。例子:

1     => 1
12 => 12
123 => 123
1234 => 1,234
12345 => 12,345

这是一件相当常见的事情,但我不知道应该使用哪个过滤器。

编辑:如果您有通用的 Python 方法来执行此操作,我很高兴在我的模型中添加格式化字段。

最佳答案

Django 贡献了 humanize应用程序这样做:

{% load humanize %}
{{ my_num|intcomma }}

确保将 'django.contrib.humanize' 添加到 settings.py 文件中的 INSTALLED_APPS 列表中。

关于python - 在 django 模板中格式化数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/346467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com