Django 模板:如果以 .0 结尾,将浮点数转换为整数? [英] Django templates: Convert float to integer if it ends with .0?

查看:12
本文介绍了Django 模板:如果以 .0 结尾,将浮点数转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Django 模板中,有没有办法将浮点数转换为整数,当且仅当它以 .0 结尾时?

In a Django template, is there a way to convert a float into an integer if and only if it ends in .0?

我有一个人口字段,它在我的数据库中是一个浮点数.有时它真的"是一个浮点数,以 0.5 结尾,但更多时候它以 .0 结尾,因为有很多人,在这些情况下,我宁愿只显示一个整数.

I have a field for population that is a float in my database. Sometimes it is 'really' a float, ending .5, but more often it ends .0, because there is a whole number of people, and in those cases I'd rather just show an integer.

{{ place.population }} people

对于在 Django 中解决这个问题的聪明方法有什么想法吗?

Any ideas for a clever way to get round this in Django?

推荐答案

您可以使用 floatformat 过滤器 带有否定参数!

You can use the floatformat filter with a negative argument!

这篇关于Django 模板:如果以 .0 结尾,将浮点数转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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