Django浮点格式只能在浮点数之后得到数字 [英] Django float format get only digits after floating point
本文介绍了Django浮点格式只能在浮点数之后得到数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个django模板过滤器,以便在浮点后只得到数字?
例如:
2.34 - > 34
2.00 - > 00
1.10 - > 10
我没有在 https://docs.djangoproject.com/en/dev/ref/templates/builtins/ 。
解决方案
除了创建自己的自定义过滤器,您可以使用 django-mathfilters
包:
{value | mod:1 | mul:100 | floatformat:0}}
其中: / p>
-
mod
是由mathfilters提供的模过滤器
-
mul
是由mathfilters
-
floatformat
是一个内置的django过滤器
演示:
>>>从django.template导入模板,上下文
>>> c = Context({'value':2.34})
>>> t = Template('{%load mathfilters%} {{value | mod:1 | mul:100 | floatformat:0}}')
>>> t.render(c)
u'34'
Is there a django template filter to get only digits after the floating point?
For example :
2.34 --> 34
2.00 --> 00
1.10 --> 10
I have not found an answer in https://docs.djangoproject.com/en/dev/ref/templates/builtins/.
解决方案
Aside from creating your own custom filter, you can solve it using django-mathfilters
package:
{{ value|mod:1|mul:100|floatformat:"0" }}
where:
mod
is a "modulo" filter provided bymathfilters
mul
is a "multiplication" filter provided bymathfilters
floatformat
is a built-in django filter
Demo:
>>> from django.template import Template, Context
>>> c = Context({'value': 2.34})
>>> t = Template('{% load mathfilters %}{{ value|mod:1|mul:100|floatformat:"0" }}')
>>> t.render(c)
u'34'
这篇关于Django浮点格式只能在浮点数之后得到数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文