Django 浮点格式只获取浮点数后的数字 [英] Django float format get only digits after floating point

查看:25
本文介绍了Django 浮点格式只获取浮点数后的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如:

2.34 -->342.00 -->001.10 -->10

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

解决方案

除了 创建您自己的自定义过滤器,您可以使用 django-mathfilters:

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

哪里:

  • modmathfilters
  • 提供的模"过滤器
  • mulmathfilters
  • 提供的乘法"过滤器
  • floatformat 是一个内置的 Django 过滤器

演示:

<预><代码>>>>从 django.template 导入模板,上下文>>>c = Context({'value': 2.34})>>>t = Template('{% load mathfilters %}{{ value|mod:1|mul:100|floatformat:"0" }}')>>>t.render(c)你'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 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'

这篇关于Django 浮点格式只获取浮点数后的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆