diff --git a/APPL/Localscripts/iface.js b/APPL/Localscripts/iface.js index 6e4c614907..8c73d179eb 100644 --- a/APPL/Localscripts/iface.js +++ b/APPL/Localscripts/iface.js @@ -810,11 +810,58 @@ function allowInputExpression(thisObj) } } +// Afronden van geldbedragen. +// roundcurr(1.275, 2); // Returns 1.28 +// roundcurr(-1.275, 2); // Returns -1.28 +// roundcurr(1.27499, 2); // Returns 1.27 +// roundcurr(-1.27499, 2); // Returns -1.27 +// roundcurr(1.2345678e+2, 2); // Returns 123.46 +// roundcurr(1234.5678, -2); // Returns 1200 +// roundcurr(123.45); // Returns 123 +function roundcurr(value, exp) { + if (typeof exp === 'undefined' || +exp === 0) + return Math.round(value); + + value = +value; + exp = +exp; + + if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) + return NaN; + + // Negatieve getallen voor het afronden even positief maken. + var negatief = false; + if (value < 0) + { + negatief = true; + value = value * -1; + } + + // Shift + value = value.toString().split('e'); + value = Math.round(+(value[0] + 'e' + (value[1]? (+value[1] + exp) : exp))); + + // Negatieve getallen weer negatief maken. + if (negatief) + value = value * -1; + + // Shift back + value = value.toString().split('e'); + value = +(value[0] + 'e' + (value[1]? (+value[1] - exp) : -exp)) + return (value).toFixed(exp); // value is al afgerond. ik kan nu gerust toFixed() gebruiken om eind nullen terug te krijgen (1.5toFixed(2) => 1.50). +} + // Voorheen num2curr.js function num2curr(s) { -// debugger; if( !isNaN(s) ) { - s = s.toFixed(2); + // MARX#58496: Be careful using ".toFixed()" as it might return different rounding results for different browsers. + // + // Afronding waarde.toFixed(2) voor verschillende browsers: + // IE: 251,075 => 251,08, + // FF,Crome: 251,075 => 251,07) + // + // Therefore we use roundcurr instead. + //s = s.toFixed(2); + s = roundcurr(s, 2); } return s; } @@ -828,7 +875,15 @@ function num2currEditable(fnum, decimals) if (typeof decimals == "undefined") decimals = 2; - fnum = fnum.toFixed(decimals); + // MARX#58496: Be careful using ".toFixed()" as it might return different rounding results for different browsers. + // + // Afronding waarde.toFixed(2) voor verschillende browsers: + // IE: 251,075 => 251,08, + // FF,Crome: 251,075 => 251,07) + // + // Therefore we use roundcurr instead. + //fnum = fnum.toFixed(decimals); + fnum = roundcurr(fnum, decimals); var waarde2 = (1.5).toLocaleString(); // Eventueel gewenste komma. fnum = fnum.substring(0, fnum.length - decimals - 1) + waarde2.substring(2, 1) + fnum.substring(fnum.length - decimals);