Skip to content

Commit

Permalink
Fixed issue Row, column, and grand totals within Array Texts shows .0…
Browse files Browse the repository at this point in the history
…6999999 when adding .01 + .06

Dev now round those sums appropriately both at setup (when returning to page) and run-time
Dev also now display individual fields with comma as radix separator if needed
  • Loading branch information
TMSWhite committed Mar 2, 2012
1 parent 326a6bb commit 972152b
Showing 1 changed file with 50 additions and 14 deletions.
64 changes: 50 additions & 14 deletions scripts/survey_runtime.js
Expand Up @@ -547,6 +547,36 @@ function std_onsubmit_handler()
return true;
}

// round function from phpjs.org
function round (value, precision, mode) {
// http://kevin.vanzonneveld.net
var m, f, isHalf, sgn; // helper variables
precision |= 0; // making sure precision is integer
m = Math.pow(10, precision);
value *= m;
sgn = (value > 0) | -(value < 0); // sign of the number
isHalf = value % 1 === 0.5 * sgn;
f = Math.floor(value);

if (isHalf) {
switch (mode) {
case 'PHP_ROUND_HALF_DOWN':
value = f + (sgn < 0); // rounds .5 toward zero
break;
case 'PHP_ROUND_HALF_EVEN':
value = f + (f % 2 * sgn); // rouds .5 towards the next even integer
break;
case 'PHP_ROUND_HALF_ODD':
value = f + !(f % 2); // rounds .5 towards the next odd integer
break;
default:
value = f + (sgn > 0); // rounds .5 away from zero
}
}

return (isHalf ? value : Math.round(value)) / m;
}

// ==========================================================
// totals

Expand Down Expand Up @@ -712,15 +742,18 @@ function multi_set(ids,_radix)
_bits[id][i].onChange = calc;
if(i == (l - 1))
{
_bits[id][i].value = qt;
_bits[id][i].value = round(qt,12)
}
else if(_bits[id][i].value)
{
qt += (_bits[id][i].value * 1);
// }
// else
// {
// _bits[id][i].value = '0';
_aval=_bits[id][i].value;
if (radix===',') {
_aval = _aval.split(',').join('.');
_bits[id][i].value = _aval.split('.').join(',');
}
if (_aval == parseFloat(_aval)) {
qt += +_aval;
}
};
};

Expand All @@ -740,15 +773,18 @@ function multi_set(ids,_radix)
_bits[i][id].onchange = calc;
if(i == (l - 1))
{
_bits[i][id].value = qt;
_bits[i][id].value = round(qt,12);
}
else if(_bits[i][id].value)
{
qt += (_bits[i][id].value * 1);
// }
// else
// {
// _bits[i][id].value = '0';
_aval=_bits[i][id].value;
if (radix===',') {
_aval = _aval.split(',').join('.');
_bits[i][id].value = _aval.split('.').join(',');
}
if (_aval == parseFloat(_aval)) {
qt += +_aval;
}
};
};
};
Expand Down Expand Up @@ -823,7 +859,7 @@ function multi_set(ids,_radix)
}
else
{
_bits[i][vid].value = qt;
_bits[i][vid].value = round(qt,12);
}
}
else if(_bits[i][vid].value)
Expand Down Expand Up @@ -855,7 +891,7 @@ function multi_set(ids,_radix)
}
else
{
_bits[hid][i].value = qt;
_bits[hid][i].value = round(qt,12);
}
}
else if(_bits[hid][i].value)
Expand Down

0 comments on commit 972152b

Please sign in to comment.