Django –在模板中删除十进制的尾随零 [英] Django – remove trailing zeroes for a Decimal in a template

查看:69
本文介绍了Django –在模板中删除十进制的尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从Django模板的十进制字段中删除尾随零?

Is there a way to remove trailing zeros from a Decimal field in a django template?

这就是我所拥有的: 0.0002559000 这就是我所需要的: 0.0002559

This is what I have: 0.0002559000 and this is what I need: 0.0002559.

有答案建议使用 floatformat 过滤器执行此操作:

There are answers suggesting to do this using the floatformat filter:

{{ balance.bitcoins|floatformat:3 }}

但是, floatformat 执行四舍五入(向下或向上),在我的情况下这是不需要的,因为我只需要删除尾随零而根本不进行四舍五入。

However, floatformat performs rounding (either down or up), which is unwanted in my case, as I only need to remove trailing zeros without any rounding at all.

推荐答案

解决方案是使用 normalize()方法:

{{ balance.bitcoins.normalize }}

这篇关于Django –在模板中删除十进制的尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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