Development Book V17: Float Fields

Odoo's float fields are primarily used to hold numerical numbers or float values; null values are not supported. The value 0.0 is returned if the field is empty. If float fields are being used, we may wish to allow the user to set the decimal accuracy. To the Library Books model, we might now include a Cost Price field with user-configurable decimal precision. It is challenging to maintain the float values' exact values (e.g., 125.325). Furthermore, the platforms and circumstances in which this type of value is used determine its value.

Decimal accuracy

Decimal accuracy is a measurement tool that allows the user to select the floating location of several categories, like price and unit of measurement. This feature makes it much easy to decide how many values in each category should be shown as decimals. To help us with this, let's go through an example.

odoo-creating-odoo-modules

When we look at the product unit of measure, we can observe that its decimal accuracy is 2.

odoo-creating-odoo-modules

We observe that the current decimal points are set at 2, despite defining a decimal accuracy of five.

odoo-creating-odoo-modules

The decimal point is five in the present case.

odoo-creating-odoo-modules

We can create the float fields by code using the code below. At default, the decimal accuracy will be 2.

fee = fields.Float(string='Fee')
odoo-creating-odoo-modules

Odoo provides multiple methods to find a value's decimal position. That is explained in more detail below.

1. float_compare(): The Odoo tools directory contains the float comparison function. This function is helpful when we need to compare two values.

2. float_is_zero(): This is a function that checks if a float is zero. If the given value can be considered zero using the precision value and rounding digits specified, this Odoo function returns true.

4. float_round(): This is also defined in float utils.py, which is located in the Odoo tools directory. After rounding the input value using the specified rounding parameters, this function returns the value that was supplied.

5. float_repr(): This method returns the textual representation of the given value with the chosen decimal precision. This is used to get the value of an afloat string. Rounding isn't a term used to describe it. It's possible that the erroneous value will be returned as a result of this. You can instead use the float round function ().

6. float_split_str(): This function is useful when you need to divide a float value from both an integer and a fraction value. It divides the given value accurately into its decimal and unitary parts. The float value is first rounded using the float round() function, which was previously covered. The rounded number is subsequently converted to a string using the float repr function (). A tuple will then be returned after the value has been divided into its integer and fractional components.

7. float_split(): This function is similar to float split str(), but instead of string values, it returns integer values.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message