Skip to Content
Menu
This question has been flagged

Hi Odoo Community :)


I've installed the OCA module for storing a product's dimensions (https://github.com/OCA/product-attribute/tree/12.0/product_dimension ) which automatically calculates the volume field (by default cubic meters). Odoo's default product.template model volume field definition excludes the digits param (scale and precision) so whilst the auto calculated volume is persisted and stored in the db it isn't showing on the product form UI for very small volumes (e.g. a phone case). I solved this by adding the scale and precision of 17 and 17, respectively (taken from db table definition), to the volume field by extending the product.template model and adding the following line:


```

    # override precision on volume field


    volume = fields.Float(

        'Volume', compute='_compute_volume', inverse='_set_volume', digits=(17, 17), 

        help="The volume in m3.", store=True)

```


As expected, the ui now shows the full 17 decimal places:


Logistics

Weight 0.00kg 
Volume 0.00135200000000000m³


While this is great there's clearly too much precision for some things resulting in meaningless 0's so I'm wondering if it's possible to right strip the 0's by applying a string function pre output render so it's dynamic per product? e.g. pythons rstrip method:


>>> s = '0.00135200000000000'

>>> s.rstrip('0')

'0.001352'


Ideally the product's volume uom would be dynamic based on the stored dimension's uom but that's for another day :)


Wish you are all safe and well during this time and thanks for any help in advance! 

Richard

Avatar
Discard
Best Answer

Hello Richard,

You can solve this by adding this file to your static folder and adding the widget to any field that you want truncated:

/** @odoo-module */

import { registry } from "@web/core/registry";
import { FloatField, floatField } from "@web/views/fields/float/float_field";
import { formatFloat } from "@web/views/fields/formatters";

export class FloatWithoutTrailingZeros extends FloatField{
get formattedValue() {
if (this\\.props\\.inputType\\ ===\\ "number"\\ \\&\\&\\ !this\\.props\\.readonly\\ \\&\\&\\ this\\.value\\)\\ \\{
\ \ \ ​​return\\ this\\.value;
\ ​\ \ \ \}
\ ​\ \ \ const\\ numberAsString\\ =\\ this.value.toString();
const commaIndex = numberAsString.indexOf('.');
let significant_digits = 0;

if (commaIndex !== -1){
const decimalPartTrimmed = numberAsString.slice(commaIndex + 1).replace(/0+$/, '');
significant_digits = decimalPartTrimmed.length
}
return formatFloat(this.value, { digits: [16, significant_digits] });
}
}

export const floatfieldnotrailingzeros = {
...floatField,
component: FloatWithoutTrailingZeros,
};

registry.category("fields").add("FloatWithoutTrailingZeros", floatfieldnotrailingzeros);

then in your xml file:

make sure that your manifest correctly imports this file in assets.backend

The field wild truncate all 0's after the record is saved.

Avatar
Discard
Related Posts Replies Views Activity
0
Mar 21
2989
3
Dec 23
49141
0
Mar 15
3396
2
Mar 15
6747
1
May 16
5623