我的问题很简单,但我找不到答案。如果之前已经回答过,请原谅。
我有一个 DecimalField,在我的 Django 应用程序中从视图传递到模板中。 我需要仅显示其整数部分。
我该怎么做?
========================= 找到解决方案 ========================
抱歉,如果我有权限的话,我会回答的,但只有等待8小时后我才能这样做。
感谢您的答案。我已经找到了一个内置的解决方案,它可以正常工作。 这是一个内置的模板过滤器。文档如下。
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#floatformat
基本上,我需要的用法类似于这样:{{decimal_obj|floatformat:0}}
一个更好的解决方案是使用stringformat过滤器:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#stringformat
使用方法:
{{decimal_obj|stringformat:"d"}}
感谢帮助。Ignacio是正确的;)