Django float 格式仅获取浮点后的数字

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/中找到答案。

ale*_*cxe 5

除了创建自己的自定义过滤器之外,您还可以使用django-mathfilters来解决它:

{{ value|mod:1|mul:100|floatformat:"0" }}
Run Code Online (Sandbox Code Playgroud)

在哪里:

  • mod是由以下提供的“模”滤波器mathfilters
  • mul是由以下提供的“乘法”过滤器mathfilters
  • floatformat是一个内置的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)