Django float format get only digits after floating point

后端 未结 3 887
误落风尘
误落风尘 2021-01-16 06:52

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
         


        
3条回答
  •  终归单人心
    2021-01-16 07:43

    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 by mathfilters
    • mul is a "multiplication" filter provided by mathfilters
    • 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'
    

提交回复
热议问题