sheri
06-13-2005, 09:50 PM
Hello There,
I use this code for my table of 10 cols and 10 rows to show the total and grand total of all the decimals that was input by user. When I try it on my form it rounds up the Total and GrandTotal with 2 decimals only. What could I add to this code to fix it.
EX: I like the Total to show 0.579 and NOT as 0.58 if users input in first column 0.123 and 0.456.
Code:
function roundXPlaces (inNum, numPlaces) {
if (isNaN(inNum)) inNum = 0;
expr = /\./;
decimalNum = new String(inNum);
ptIndex = decimalNum.search(expr);
if (ptIndex == -1) {
rightSide = "";
leftSide = decimalNum;
} else {
rightSide = decimalNum.substr(ptIndex + 1, decimalNum.length - ptIndex - 1);
leftSide = decimalNum.substr(0, ptIndex);
}
if (leftSide.length == 0) {
leftSide = "0";
}
if (rightSide.length <= numPlaces) {
if (numPlaces == 0) return leftSide;
for (var i = rightSide.length; i < numPlaces; i++) rightSide += "0";
return leftSide + "." + rightSide;
} else {
if (rightSide.substr(numPlaces, 1) > 4) {
if (numPlaces == 0) return eval(leftSide) + 1;
else {
var newArr = rightSide.substr(0, numPlaces).split("");
newArr[newArr.length-1] = eval(newArr[newArr.length-1])+1;
for (var j = newArr.length-1; j >= 0; j--) {
if (eval(newArr[j]) == 10) {
if (j == 0) leftSide = eval(leftSide)+1;
else newArr[j-1] = eval(newArr[j-1])+1;
newArr[j] = "0";
}
}
rightSide = newArr.join("");
return leftSide + "." + rightSide;
}
} else {
if (numPlaces == 0) return leftSide;
else return leftSide + "." + rightSide.substr(0, numPlaces);
}
}
}
I use this code for my table of 10 cols and 10 rows to show the total and grand total of all the decimals that was input by user. When I try it on my form it rounds up the Total and GrandTotal with 2 decimals only. What could I add to this code to fix it.
EX: I like the Total to show 0.579 and NOT as 0.58 if users input in first column 0.123 and 0.456.
Code:
function roundXPlaces (inNum, numPlaces) {
if (isNaN(inNum)) inNum = 0;
expr = /\./;
decimalNum = new String(inNum);
ptIndex = decimalNum.search(expr);
if (ptIndex == -1) {
rightSide = "";
leftSide = decimalNum;
} else {
rightSide = decimalNum.substr(ptIndex + 1, decimalNum.length - ptIndex - 1);
leftSide = decimalNum.substr(0, ptIndex);
}
if (leftSide.length == 0) {
leftSide = "0";
}
if (rightSide.length <= numPlaces) {
if (numPlaces == 0) return leftSide;
for (var i = rightSide.length; i < numPlaces; i++) rightSide += "0";
return leftSide + "." + rightSide;
} else {
if (rightSide.substr(numPlaces, 1) > 4) {
if (numPlaces == 0) return eval(leftSide) + 1;
else {
var newArr = rightSide.substr(0, numPlaces).split("");
newArr[newArr.length-1] = eval(newArr[newArr.length-1])+1;
for (var j = newArr.length-1; j >= 0; j--) {
if (eval(newArr[j]) == 10) {
if (j == 0) leftSide = eval(leftSide)+1;
else newArr[j-1] = eval(newArr[j-1])+1;
newArr[j] = "0";
}
}
rightSide = newArr.join("");
return leftSide + "." + rightSide;
}
} else {
if (numPlaces == 0) return leftSide;
else return leftSide + "." + rightSide.substr(0, numPlaces);
}
}
}