在django模板中格式化数字 [英] Format numbers in django templates

查看:312
本文介绍了在django模板中格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

它是一个相当常见的事情,但我无法确定哪个过滤器我' m应该使用。



编辑:如果你有一个通用的Python方法来做到这一点,我很高兴在我的模型中添加一个格式化的字段。

解决方案

Django的贡献 humanize 应用程序执行此操作:

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

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


I'm trying to format numbers. Examples:

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

It strikes as a fairly common thing to do but I can't figure out which filter I'm supposed to use.

Edit: If you've a generic Python way to do this, I'm happy adding a formatted field in my model.

解决方案

Django's contributed humanize application does this:

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

Be sure to add 'django.contrib.humanize' to your INSTALLED_APPS list in the settings.py file.

这篇关于在django模板中格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆