Fixes issue 29113: BigDecimal is used to prevent rounding error
authorAugusto Mauch <augusto.mauch@openbravo.com>
Tue, 03 Mar 2015 19:14:50 +0100
changeset 26117 b5096788cd44
parent 26116 39b20a8c12e9
child 26118 afec99f5af43
child 26120 ece5f469f671
Fixes issue 29113: BigDecimal is used to prevent rounding error

There was a problem with javascript decimal precision that was affecting the way we round numbers using the OB.Utilities.Number.roundJSNumber. For instance, if this is 0.145*100 is evaluated in the javascr
ipt console, the result was 14.499999999999998, not 14.5.

Now the OB.Utilities.Number.roundJSNumber uses BigDecimal to overcome this precision error. This is the command used not to round a number:

parseFloat(new BigDecimal(num).round(dec,BigDecimal.prototype.ROUND_HALF_UP));
modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities-number.js
--- a/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities-number.js	Tue Mar 03 21:33:25 2015 +0000
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities-number.js	Tue Mar 03 19:14:50 2015 +0100
@@ -35,8 +35,8 @@
 // Return:
 // * The rounded JS number
 OB.Utilities.Number.roundJSNumber = function (num, dec) {
-  var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
-  return result;
+  var strNum = (isc.isA.String(num) ? num : String(num));
+  return parseFloat(new BigDecimal(strNum).round(dec, BigDecimal.prototype.ROUND_HALF_UP));
 };
 
 // ** {{{ OB.Utilities.Number.OBMaskedToOBPlain }}} **