我有一个从数据库获取数据的变量
{{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/