截断floatfield的十进制数字总是四舍五入 [英] Truncate decimal numbers of floatfield always rounding down

查看:66
本文介绍了截断floatfield的十进制数字总是四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个浮点字段,我试图只显示小数点前的数字

I have a float field and Im trying to only show the digit in front of the decimal point

因此 8.70 将显示为 8 .如果值为 9.00 ,则应显示为 9

so 8.70 will display as 8. If the value is 9.00, it should display as 9

当我尝试使用 | floatformat:"0" 8.70 将显示为 9

When I try to use |floatformat:"0" 8.70 will display as 9

我如何确保它永远不会舍入?

How can I make sure it never rounds up?

推荐答案

我认为您找不到想要的内置模板标签.

I don't think there's a built-in template tag for what you seek.

从我的角度来看,您有两个选择.

The way I see it you have two options.

如果该值不会在页面上的其他任何地方使用,则可以通过更改上下文字典来更改将其传递给模板的方式

If the value won't be used anywhere else on your page, you could just change the way it's passed on to the template by changing the context dict

return render(request, 'templates/my_template.html', {'my_value': math.floor(my_value)})

否则,您可以构建自己的模板标签.

Otherwise, you can build your own template tag.

@register.filter
def floor_floats(value):
    return math.floor(value)

然后在模板上使用它:

{{ value|floor_floats }}

希望这会有所帮助

这篇关于截断floatfield的十进制数字总是四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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