我试图在模板中将十进制值表示为百分比。具体地说,我在一个模型中有几个字段的值,比如'0.4000000059604645‘。在models.py中,我将其表示为
information_percent = models.FloatField(blank=True)在某种程度上,我有:
org = Organizationaldata.objects.get(name=organization)
information_percent = org.information_percent我不明白的是如何让它在模板中显示为,例如,'40.0‘。我已经做了很多搜索,但我不知道如何将值乘以100,或者其他一些可以提供预期结果的技术。
我们将非常感谢您的建议。
发布于 2011-02-10 10:55:58
听起来你需要built-in floatformat template filter。
例如:
views.py
org = Organizationaldata.objects.get(name=organization)
information_percent = 100 * org.information_percenttemplate.html
<span>Info percent: {{ information_percent|floatformat:1 }}</span>发布于 2011-02-10 10:52:57
也许这就是你想要的?
information_percent = int(org.information_percent*100)如果您想要小数部分,可以将其扩展为:
information_percent = int(org.information_percent*100) + (org.information_percent*100 - int(org.information_percent*100))https://stackoverflow.com/questions/4952880
复制相似问题