django 模板中的乘法而不使用手动创建的模板标签 [英] multiplication in django template without using manually created template tag

查看:17
本文介绍了django 模板中的乘法而不使用手动创建的模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在django模板中实现乘法运算.例如我有价值观,价格=10.50数量=3

I want to achieve multiplication operation in django template. For example I have the values, price=10.50 quantity=3

借助此链接

http://slacy.com/blog/2010/07/using-djangos-widthratio-template-tag-for-multiplication-division/

我尝试了以下代码来实现它,

i tried below codes for achieving it,

{% widthratio quantity 1 price %}

但它只返回 31.但我需要浮点数 (31.5) 的答案

but its returning only 31. But i need the answer in float (31.5)

我想在不使用手动创建的标签的情况下实现它

And i want to achieve it without using the manually created tags

我怎样才能实现它?提前致谢...

How can i achieve it? Thanks in advance...

推荐答案

您可以使用内置的 widthratio 模板标签.

You can use the built-in widthratio template tag.

  • a*b 使用 {% widthratio a 1 b %}
  • a/b 使用 {% widthratio a b 1 %}

注意:结果在返回前四舍五入为整数.

Note: the results are rounded to an integer before returning.

@see https://docs.djangoproject.com/en/dev/ref/templates/builtins/

这篇关于django 模板中的乘法而不使用手动创建的模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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