Fixes issue 29113: Use setScale instead of round, and handle properly NaN
authorAugusto Mauch <augusto.mauch@openbravo.com>
Thu, 05 Mar 2015 10:15:41 +0100
changeset 26126 e9d73be9949d
parent 26125 86a6b64eb747
child 26127 1c75a6afca4e
child 26130 2369c54c9f14
Fixes issue 29113: Use setScale instead of round, and handle properly NaN

The BigDecimal.round function did not work as we expected [1]:

new BigDecimal('0.145').round(2, BigDecimal.prototype.ROUND_HALF_UP).toString() -> 0.15 (OK)
new BigDecimal('1.145').round(2, BigDecimal.prototype.ROUND_HALF_UP).toString() -> 1.1 (WRONG)
new BigDecimal('10.145').round(2, BigDecimal.prototype.ROUND_HALF_UP).toString() -> 10 (WRONG)

To round the number to a specific amout of decimal digits, the setScale function should be used.

new BigDecimal('0.145').setScale(2, BigDecimal.prototype.ROUND_HALF_UP).toString() -> 0.15 (OK)
new BigDecimal('1.145').setScale(2, BigDecimal.prototype.ROUND_HALF_UP).toString() -> 1.15 (OK)
new BigDecimal('10.145').setScale(2, BigDecimal.prototype.ROUND_HALF_UP).toString() -> 10.15 (OK)

Also, when the number provided in the first argument of the OB.Utilities.Number.roundJSNumber is not a number, the function returns NaN, as it used to do it before.

[1] http://stackoverflow.com/a/13461270
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	Wed Mar 04 18:51:22 2015 +0100
+++ b/modules/org.openbravo.client.application/web/org.openbravo.client.application/js/utilities/ob-utilities-number.js	Thu Mar 05 10:15:41 2015 +0100
@@ -35,8 +35,12 @@
 // Return:
 // * The rounded JS number
 OB.Utilities.Number.roundJSNumber = function (num, dec) {
-  var strNum = (isc.isA.String(num) ? num : String(num));
-  return parseFloat(new BigDecimal(strNum).round(dec, BigDecimal.prototype.ROUND_HALF_UP));
+  var strNum;
+  if (isNaN(num)) {
+    return NaN;
+  }
+  strNum = (isc.isA.String(num) ? num : String(num));
+  return parseFloat(new BigDecimal(strNum).setScale(dec, BigDecimal.prototype.ROUND_HALF_UP));
 };
 
 // ** {{{ OB.Utilities.Number.OBMaskedToOBPlain }}} **