django - django中小数点前分割

标签 django django-templates

我有一个从数据库获取数据的变量

{{i.rewardpoints}}

以及它返回的值,例如 1.799 或 12,数据库有多个包含小数和不带小数的值

但我需要显示不带小数的值

我该怎么做

最佳答案

四舍五入到最接近的整数:

{{ i.rewardpoints|floatformat:"0" }}

获取整数部分:

{{ i.rewardpoints|stringformat:"d" }}

In [19]: tpl = Template('{{ x|stringformat:"d" }} {{ x|floatformat:"0" }}')

In [20]: tpl.render(Context({'x': 1.1}))
Out[20]: u'1 1'

In [21]: tpl.render(Context({'x': 1.9}))
Out[21]: u'1 2'

关于django - django中小数点前分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15743597/

相关文章:

python - Django - 使 ModelForm(属于 ImageField)只接受某些类型的图像

python - Django - 如何使用 sorl-thumbnail?

django - 如何在模板中获取当前页面的URL,包括参数?

Django - 如何将多个参数传递给 url 模板标签

javascript - 如何在 Django 中应用 csrf_token

django - 如何访问 Django 模板中的动态 key ?

python - Django错误: Not environment configured

python - Django CMS 是否在 Google App Engine 上运行?

python - 联结表(多对多)或自定义字段类型上的多对一关系?

python - 如何在单击按钮时调用 Django 函数?