How to add decimals in calculations
This question has been flagged
1
Reply
1004
Views
Hi,
1. Define Fields with Precision.
Use fields.Float for general decimal numbers, specifying precision with the digits attribute (e.g., digits=(16, 2) for 2 decimal places, or digits='Product Price' for named precision).
Use fields.Monetary for currency amounts, which automatically uses the precision of the associated currency_id field.
2.Perform Calculations Safely:
Odoo's ORM generally handles underlying decimal.Decimal objects for precision.
For explicit rounding in Python code, use odoo.tools.float_round(value, precision_digits=X) to ensure consistent rounding behavior.
3.Display Decimals in Views and Reports:
XML views and QWeb reports automatically apply the precision defined on the field.
Use widget="monetary" in XML for monetary fields to ensure proper currency formatting and decimal display.
In QWeb, t-field with t-options='{"widget": "monetary", "display_currency": record.currency_id}' or t-options='{"widget": "float", "precision": X}' can be used for precise display.
Hope it helps.
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up