diff options
| author | niv-openerp <nicolas.vanhoren@openerp.com> | 2013-07-25 12:07:49 +0200 |
|---|---|---|
| committer | niv-openerp <nicolas.vanhoren@openerp.com> | 2013-07-25 12:07:49 +0200 |
| commit | 42e493e73efd603002feb92703a82f34c6bc6aea (patch) | |
| tree | 32a49385764688e013f12d849231996c9d8f0358 /addons/web/static/src/js/formats.js | |
| parent | b105770bbc0f545a4dfd0482b5b310f534fc1033 (diff) | |
Added Gruntfile with jshint and corrected most js errors
bzr revid: nicolas.vanhoren@openerp.com-20130725100749-0i82gf3k3nvt4r44
Diffstat (limited to 'addons/web/static/src/js/formats.js')
| -rw-r--r-- | addons/web/static/src/js/formats.js | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js index 851b60af229..f4df990675c 100644 --- a/addons/web/static/src/js/formats.js +++ b/addons/web/static/src/js/formats.js @@ -141,6 +141,7 @@ instance.web.format_value = function (value, descriptor, value_if_empty) { return ''; } console.warn('Field', descriptor, 'had an empty string as value, treating as false...'); + return value_if_empty === undefined ? '' : value_if_empty; case false: case Infinity: case -Infinity: @@ -219,9 +220,9 @@ instance.web.parse_value = function (value, descriptor, value_if_empty) { case "": return value_if_empty === undefined ? false : value_if_empty; } + var tmp; switch (descriptor.widget || descriptor.type || (descriptor.field && descriptor.field.type)) { case 'integer': - var tmp; do { tmp = value; value = value.replace(instance.web._t.database.parameters.thousands_sep, ""); @@ -231,7 +232,7 @@ instance.web.parse_value = function (value, descriptor, value_if_empty) { throw new Error(_.str.sprintf(_t("'%s' is not a correct integer"), value)); return tmp; case 'float': - var tmp = Number(value); + tmp = Number(value); if (!isNaN(tmp)) return tmp; @@ -314,4 +315,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)); +}; + }; |
