From d17f22cde77fd6dd0fb968b1b34883c56648e93e Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Wed, 28 Jan 2015 14:57:12 +0100 Subject: [PATCH] [FIX] web: float compare, float is zero When setting a float field, the web client rounds the value entered by the user using the instance.web.round_decimals method. Nevertheless, this is possible that this method returns unrounded float, due to the float precision For example, round_decimals(53.8, 2) returns 53.800000000000004 In order to compare this new float value to the old float value and check the eventual change), we need to check if the delta between the two is almost 0. This is the goal of the float_is_zero method introduced during this rev. which is comparable to the float_is_zero method already available in the openerp server, in openerp.tools. Float values compared using the simple === operator instead of this float_is_zero method will be regarded as different in some cases, according to the float value and the precision And the value of the field will be regarded as changed, which can lead to unwanted triggers of onchanges. opw-626635 --- addons/web/static/src/js/formats.js | 5 +++++ addons/web/static/src/js/openerpframework.js | 3 +++ 2 files changed, 8 insertions(+) diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js index 4766fe33433..54f74f29fbf 100644 --- a/addons/web/static/src/js/formats.js +++ b/addons/web/static/src/js/formats.js @@ -368,4 +368,9 @@ instance.web.round_decimals = function(value, decimals){ return instance.web.round_precision(value, Math.pow(10,-decimals)); }; +instance.web.float_is_zero = function(value, decimals){ + epsilon = Math.pow(10, -decimals); + return Math.abs(instance.web.round_precision(value, epsilon)) < epsilon; +}; + })(); diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 007edae7220..ed80a3abc87 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -468,6 +468,9 @@ openerp.PropertiesMixin = _.extend({}, openerp.EventDispatcherMixin, { var tmp = self.__getterSetterInternalMap[key]; if (tmp === val) return; + if (self.field && self.field.type === 'float' && tmp && val && openerp.web.float_is_zero(tmp - val, self.field.digits[1])){ + return; + } changed = true; self.__getterSetterInternalMap[key] = val; if (! options.silent)