summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js
diff options
context:
space:
mode:
authorFrédéric van der Essen <fva@openerp.com>2013-04-09 16:44:14 +0200
committerFrédéric van der Essen <fva@openerp.com>2013-04-09 16:44:14 +0200
commita5e4d757a19eaac32f598e9cfcbe854fa86b974f (patch)
tree8e8fe218aaa5a3409559eb2572ebfa7a64ec3f0f /addons/web/static/src/js
parent6fe10fc8e5abc6e5d66fc7ab52c2011037819ba9 (diff)
[FIX] add a correct rounding algorithm to be able to duplicate server-side financial computations
lp bug: https://launchpad.net/bugs/1157761 fixed bzr revid: fva@openerp.com-20130409144414-nqnmkny5cxrmyru1
Diffstat (limited to 'addons/web/static/src/js')
-rw-r--r--addons/web/static/src/js/formats.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js
index 851b60af229..854c5f76218 100644
--- a/addons/web/static/src/js/formats.js
+++ b/addons/web/static/src/js/formats.js
@@ -314,4 +314,34 @@ instance.web.auto_date_to_str = function(value, type) {
}
};
+/**
+ * performs a half up rounding with arbitrary precision, correcting for float loss of precision
+ * See the corresponding float_round() in server/tools/float_utils.py for more info
+ * @param {Number} the value to be rounded
+ * @param {Number} a non zero precision parameter. eg: 0.01 rounds to two digits.
+ */
+instance.web.round_precision = function(value, precision){
+ if(!value){
+ return 0;
+ }else if(!precision){
+ throw new Error('round_precision(...): Cannot round value: '+value+' with a precision of zero (or undefined)');
+ }
+ var normalized_value = value / precision;
+ var epsilon_magnitude = Math.log(Math.abs(normalized_value))/Math.log(2);
+ var epsilon = Math.pow(2, epsilon_magnitude - 53);
+ normalized_value += normalized_value >= 0 ? epsilon : -epsilon;
+ var rounded_value = Math.round(normalized_value);
+ return rounded_value * precision;
+};
+
+/**
+ * performs a half up rounding with a fixed amount of decimals, correcting for float loss of precision
+ * See the corresponding float_round() in server/tools/float_utils.py for more info
+ * @param {Number} the value to be rounded
+ * @param {Number} the number of decimals. eg: round_decimals(3.141592,2) -> 3.14
+ */
+instance.web.round_decimals = function(value, decimals){
+ return instance.web.round_precision(value, Math.pow(10,-decimals));
+};
+
};