简体   繁体   English

jinja 2 中的舍入十进制数

[英]Round Decimal number in jinja 2

I am trying to round a decimal number this is my number and my code我试图四舍五入一个十进制数这是我的号码和我的代码

68.125 
the output should be 68.13 and I get 68.12

code:代码:

{% set v_cuotas = doc.saldo_contrato/doc.plazo %}
VALUE: <span class="texto_liviano"></span>{{v_cuotas|round(2)|float}}<br>

Thanks in advance提前致谢

A minimal example, that produces the desired rounding looks like this:产生所需舍入的最小示例如下所示:

>>> from decimal import Decimal, ROUND_HALF_UP
>>> Decimal(Decimal('68.125').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))                               
Decimal('68.13')

You can also write acustom Jinja filter if you want use it the same way like the built in round filter.如果您想像内置round过滤器一样使用它,您也可以编写自定义 Jinja 过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM