poi*_*rez 1 python django floating-point django-templates django-template-filters
是否有 django 模板过滤器只获取浮点之后的数字?
例如 :
2.34 --> 34
2.00 --> 00
1.10 --> 10
Run Code Online (Sandbox Code Playgroud)
我没有在https://docs.djangoproject.com/en/dev/ref/templates/builtins/中找到答案。
除了创建自己的自定义过滤器之外,您还可以使用django-mathfilters包来解决它:
{{ value|mod:1|mul:100|floatformat:"0" }}
Run Code Online (Sandbox Code Playgroud)
在哪里:
mod是由以下提供的“模”滤波器mathfiltersmul是由以下提供的“乘法”过滤器mathfiltersfloatformat是一个内置的django过滤器演示:
>>> 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'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2737 次 |
| 最近记录: |