[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
This commit is contained in:
Denis Ledoux 2015-01-28 14:57:12 +01:00
parent a8edd217a0
commit d17f22cde7
2 changed files with 8 additions and 0 deletions

View File

@ -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;
};
})();

View File

@ -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)