How to set global rounding for DecimalFields in Dj

2019-09-19 18:21发布

I found similar question with answers, but "setting the option in the settings.py" and "setting getcontext() in apps.py" doesn't work. Standard decimal rounding is ROUND_HALF_EVEN, but I need to set ROUND_HALF_UP.

And I repeat my question with a quote - "where in the Django application I have to set rounding option, so that it would work globally in the project?"

1条回答
Evening l夕情丶
2楼-- · 2019-09-19 19:12

For django project can work setting decimal.DefaultContext (py3, py2).

This context is most useful in multi-threaded environments.

This is my code from settings.py:

import decimal
# Set global decimal rounding to ROUND_HALF_UP (instead of ROUND_HALF_EVEN).
project_context = decimal.getcontext()
project_context.rounding = decimal.ROUND_HALF_UP
decimal.DefaultContext = project_context
查看更多
登录 后发表回答