PDA

View Full Version : To Show Decimals


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);
}
}
}

looka
06-13-2005, 10:14 PM
hmmm... i dont really know what do u want, but tell me something - why do you simply dont use function like that:

function roundX(x,n){
//u should make sure that n is integer:
n = Math.pow(10,Math.abs(Math.ceil(n)));
return Math.round(x*n)/n;
}

well... im not sure if this works...:)

vwphillips
06-13-2005, 10:38 PM
no time to study your form just now but


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
function GetDec(obj){
ips=document.getElementById('Dec').getElementsByTagName('INPUT');
Total=0;
for (i=0;i<ips.length-1;i++){
if (!isNaN(ips[i].value)&&ips[i].value.indexOf('.')>=0){
Nu=parseFloat(ips[i].value.substring(ips[i].value.indexOf('.'),ips[i].value.length));
if (!isNaN(Nu)){
Total+=Nu;
}
}
}
ips[ips.length-1].value=Total;
}
//-->
</script></head>

<body>
<form id="Dec" >
<input name="" size="10" onkeyup="GetDec(this.value);" ><br>
<input name="" size="10" onkeyup="GetDec(this.value);" ><br>
<input name="" size="10" onkeyup="GetDec(this.value);" ><br>
<input name="" size="10" onkeyup="GetDec(this.value);" ><br>
<input name="" size="10" onkeyup="GetDec(this.value);" ><br>
<input name="" size="10" onkeyup="GetDec(this.value);" ><br>
<input name="" size="10" onkeyup="GetDec(this.value);" ><br>
<input name="" size="10" disabled="disabled" >
<br>
</form>

</body>

</html>

looka
06-13-2005, 10:46 PM
nevermind... i missed the point:)

martin_narg
06-14-2005, 12:10 AM
http://www.codingforums.com/showthread.php?p=178077#post178077