Skip to Content
Menu
This question has been flagged
3 Replies
5561 Views

hello guys, i have a problem to override the float fields about digit decimals, i want the digit disappears globally if the value is zero like examples: 

  • 5.660 -> 5.66

  • 5.00 -> 5

  • 7.10 -> 7.1

I'm already trying to override methods  in Class Float(Field) and nothing is happening:

  • convert_to_column

  • convert_to_cache 

this is my code:

from odoo.fields import Float


def new_float_convert_to_column(self, value, record, values=None):

    print(value, 'VALUE FLOAT MIAW')    

    print('miaw')    

    miaw1 = '{:g}'.format(float(value or 0.0))   

    result = float(miaw1)   

    return result


def new_float_convert_to_cache(self, value, record, validate=True):

    value = float(value or 0.0)

    if not validate:

        return value

    miaw = '{:g}'.format(value)

    result = float(miaw)

    return result


Float.convert_to_column = new_float_convert_to_column

Float.convert_to_cache = new_float_convert_to_cache


when i try to print the result it works! but on the odoo GUI still show 150.10 when it should be 150.1

thanks in advance

can i do that?

Avatar
Discard
Author

or am i do it wrong?

Best Answer

it will not work because of GUI (browser/client) has its own format mechanism 
https://github.com/odoo/odoo/blob/11.0/addons/web/static/src/js/fields/field_utils.js#L171

I think you do not require any server change just change the presentation layer (I mean web client javascript and ir_qweb for website)

post here if you need more help :)


Avatar
Discard
Author

thanks it because of you i can custom it :)

Author Best Answer

odoo.define('warpin_decimal_precision.float_field_utils', function (require) {

    "use strict";
    var field_utils = require('web.field_utils');

    var core = require('web.core');

    var utils = require('web.utils');
    var _t = core._t;

    var QWeb = core.qweb;


    function MyCustomformatFloat(value, field, options) {

        // Copy original function with your modification

        if (value === false) {

            return "";

        }

        var l10n = core._t.database.parameters;

        var precision;

        if (options && options.digits) {

            precision = options.digits[1];

        } else if (field && field.digits) {

            precision = field.digits[1];

        } else {

            precision = 2;

        }

        var formatted = _.str.sprintf('%.' + precision + 'f', value || 0).split('.');

        formatted[0] = utils.insert_thousand_seps(formatted[0]);

        var result = formatted.join(l10n.decimal_point);

        var final_result = String(parseFloat(result));

        return final_result

    }
    field_utils.format.float = MyCustomformatFloat;
}); 


got the answer, solved ;) 

Avatar
Discard
Related Posts Replies Views Activity
8
Dec 22
12180
3
Feb 21
16404
1
Nov 19
2496
4
Sep 18
3700
1
Aug 18
6743