我有一个浮点字段,我试图只显示小数点前的数字
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的十进制数字总是四舍五入
发布评论