Skip to content Skip to sidebar Skip to footer

How To Set Global Rounding For Decimalfields In Django Project?

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,

Solution 1:

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
# Setglobaldecimal 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

Post a Comment for "How To Set Global Rounding For Decimalfields In Django Project?"