截断floatfield的十进制数字总是四舍五入

编程入门 行业动态 更新时间:2024-10-19 02:20:57
本文介绍了截断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的十进制数字总是四舍五入

本文发布于:2023-11-10 02:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:四舍五入   十进制数   floatfield

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!