gpt4 book ai didi

python - Django float 格式只得到 float 后的数字

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:58 26 4
gpt4 key购买 nike

是否有 django 模板过滤器只获取 float 后的数字?

例如:

2.34 --> 34
2.00 --> 00
1.10 --> 10

我在 https://docs.djangoproject.com/en/dev/ref/templates/builtins/ 中没有找到答案.

最佳答案

除了creating your own custom filter ,您可以使用 django-mathfilters package 解决它:

{{ value|mod:1|mul:100|floatformat:"0" }}

哪里:

  • modmathfilters
  • 提供的“取模”过滤器
  • mulmathfilters
  • 提供的“乘法”过滤器
  • 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'

关于python - Django float 格式只得到 float 后的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27403675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com