Django模板:如果以.0结尾,则将float转换为整数 [英] Django templates: Convert float to integer if it ends with .0?
本文介绍了Django模板:如果以.0结尾,则将float转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Django模板中,当且仅当以.0结尾时,是否有一种将float转换为整数的方法?
In a Django template, is there a way to convert a float into an integer if and only if it ends in .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结尾,则将float转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文