Sum of a column (in a Table)
Collapse
X
-
Thankyou, I had to amend slightly (one * 1) + etc but this works great:Originally posted by acodertoFixed is used on a number, so you could do something like:
[CODE=javascript]total = one + two + three;
total = total.toFixed(2 );
document.getEle mentById("Total Value").innerHT ML = total;[/CODE]
[CODE=javascript]
<script type="text/javascript">
function startCalc()
{
one = document.getEle mentById("info" ).value;
two = document.getEle mentById("info1 ").value;
three = document.getEle mentById("info2 ").value;
total = (one * 1) + (two * 1) + (three * 1);
total = total.toFixed(2 );
four = document.getEle mentById("Total Value").innerHT ML = total;
totalvat = (four / 100 * 17.5) + (four * 1);
totalvat = totalvat.toFixe d(2);
five = document.getEle mentById("Total Vat").innerHTM L = totalvat;
}
</script>
[/CODE]
My biggest problem was how to tell it the number toFix was TotalValueLeave a comment:
-
toFixed is used on a number, so you could do something like:Originally posted by SteveeSorry, I did no better than a "D" in GCE Math some 31 years ago, and that lot just wasted me. can you assist with the toFix please
[CODE=javascript]total = one + two + three;
total = total.toFixed(2 );
document.getEle mentById("Total Value").innerHT ML = total;[/CODE]Leave a comment:
-
That's a floating point accuracy problem - see link. Use toFixed() to fix it to 2 decimal places.Originally posted by SteveeGotcha, thankyou.
But why when it adds up 2 of the combo numbers ie 19.52 + 100 it equals 119.52
However when I add 3 of the combo numbers ie 19.52 + 100 + 99 it equals 218.51999999999 998Leave a comment:
-
Gotcha, thankyou.Originally posted by acoderInstead of a space for the default option values, put 0.
But why when it adds up 2 of the combo numbers ie 19.52 + 100 it equals 119.52
However when I add 3 of the combo numbers ie 19.52 + 100 + 99 it equals 218.51999999999 998Leave a comment:
-
Instead of a space for the default option values, put 0.Originally posted by SteveeRight, I have tried that again and the TotalValue gives me the NaNLeave a comment:
-
Right, I have tried that again and the TotalValue gives me the NaNOriginally posted by acoderYou don't need 'mylist=' and you can remove the parseFloat when totalling because they've already been parsed.
[CODE=javascript]
<script type="text/javascript">
function startCalc()
{
one = parseFloat(docu ment.getElement ById("info").va lue);
two = parseFloat(docu ment.getElement ById("info1").v alue);
three = parseFloat(docu ment.getElement ById("info2").v alue);
document.getEle mentById("Total Value").innerHT ML = (one * 1) + (two * 1) + (three * 1);
}
</script>
[/CODE]Leave a comment:
-
You don't need 'mylist=' and you can remove the parseFloat when totalling because they've already been parsed.Originally posted by Stevee[code=javascript]
<script type="text/javascript">
function startCalc()
{
one = parseFloat(myli st=document.get ElementById("in fo").value);
two = parseFloat(myli st=document.get ElementById("in fo1").value);
three = parseFloat(myli st=document.get ElementById("in fo2").value);
document.getEle mentById("Total Value").innerHT ML = parseFloat(one + two + three);
}
</script>
[/CODE]
Now I get the dreaded - NaNLeave a comment:
-
[code=javascript]Originally posted by acoderPut it around each value. You could put it on the previous three lines, e.g.
[code=javascript]one = parseFloat(docu ment.getElement ById("info").va lue);[/code]
See the examples in the link. Use it on the TotalValue.
<script type="text/javascript">
function startCalc()
{
one = parseFloat(myli st=document.get ElementById("in fo").value);
two = parseFloat(myli st=document.get ElementById("in fo1").value);
three = parseFloat(myli st=document.get ElementById("in fo2").value);
document.getEle mentById("Total Value").innerHT ML = parseFloat(one + two + three);
}
</script>
[/CODE]
Now I get the dreaded - NaNLeave a comment:
-
Put it around each value. You could put it on the previous three lines, e.g.Originally posted by SteveeOK, total lack of knowledge again, where on line 30
I currently have placed it here:
document.getEle mentById("Total Value").innerHT ML = parseFloat(one + two + three);
which still works and has reduced the decimals to 4 places.
[code=javascript]one = parseFloat(docu ment.getElement ById("info").va lue);[/code]
See the examples in the link. Use it on the TotalValue.Originally posted by SteveeIs this Tofixed another script or does it fit neatly (somewhere) into the:
<td id="TotalValue " width="75" align="right">Leave a comment:
-
OK, total lack of knowledge again, where on line 30Originally posted by acoderOn line 30, the 'correct' way to parse strings for numbers is to use parseFloat(). If you do have problems with decimal places, use toFixed on modern browsers.
I currently have placed it here:
document.getEle mentById("Total Value").innerHT ML = parseFloat(one + two + three);
which still works and has reduced the decimals to 4 places.
Is this Tofixed another script or does it fit neatly (somewhere) into the:
<td id="TotalValue " width="75" align="right">Leave a comment:
-
On line 30, the 'correct' way to parse strings for numbers is to use parseFloat(). If you do have problems with decimal places, use toFixed on modern browsers.Originally posted by SteveeThank you it now works a treat.
Except, the combo <TD>'s have 2 decimal places and the TotalValue has quite a few more, even though I only put in whole pence?Leave a comment:
-
Thank you it now works a treat.
Except, the combo <TD>'s have 2 decimal places and the TotalValue has quite a few more, even though I only put in whole pence?
Regards, Stevee.
[CODE=javascript]
<html>
<head><title>?? ?</title>
<script type="text/javascript">
function getinfo()
{
var mylist=document .getElementById ("info")
document.getEle mentById("displ ayinfo").innerH TML=mylist.opti ons[mylist.selected Index].value
}
function getinfo1()
{
var mylist=document .getElementById ("info1")
document.getEle mentById("displ ayinfo1").inner HTML=mylist.opt ions[mylist.selected Index].value
}
function getinfo2()
{
var mylist=document .getElementById ("info2")
document.getEle mentById("displ ayinfo2").inner HTML=mylist.opt ions[mylist.selected Index].value
}
</script>
<script type="text/javascript">
function startCalc()
{
one = mylist=document .getElementById ("info").val ue;
two = mylist=document .getElementById ("info1").value ;
three = mylist=document .getElementById ("info2").value ;
document.getEle mentById("Total Value").innerHT ML = (one * 1) + (two * 1) + (three * 1);
}
</script>
</head>
<body>
<table border="1">
<tr>
<td width="95" align="right">
<select id="info" onchange="getin fo();startCalc( )">
<option selected value="12">Sele ct</option>
<option value="19.00">U nit 1</option>
<option value="22.00">U nit 2</option>
<option value="25.00">U nit 3</option>
<option value="29.00">U nit 4</option>
<option value="32.00">U nit 5</option>
</select>
</td>
<td width="75" align="right" id="displayinfo "> </td>
</tr>
<tr>
<td width="95" align="right">
<select id="info1" onchange="getin fo1();startCalc ()">
<option selected value=" ">Select</option>
<option value="84.00">N ext 1</option>
<option value="85.00">N ext 2</option>
<option value="86.00">N ext 3</option>
<option value="87.00">N ext 4</option>
<option value="88.00">N ext 5</option>
</select>
</td>
<td width="75" align="right" id="displayinfo 1"> </td>
</tr>
<tr>
<td width="95" align="right">
<select id="info2" onchange="getin fo2();startCalc ()">
<option selected value=" ">Select</option>
<option value="99.00">A gain 1</option>
<option value="92.00">A gain 2</option>
<option value="95.00">A gain 3</option>
<option value="99.00">A gain 4</option>
<option value="92.00">A gain 5</option>
</select>
</td>
<td width="75" align="right" id="displayinfo 2"> </td>
</tr>
<tr>
<td width="95" align="right">T otal = </td>
<td id="TotalValue " width="75" align="right" value=""> </td>
</tr>
</table>
</body>
</html>
[/CODE]Leave a comment:
-
A td element does not have a value attribute. Use innerHTML instead:Originally posted by SteveeRight I have been giving that lot a go but still don't think I understand (obviously)
[CODE=javascript]document.getEle mentById("Total Value").innerHT ML[/CODE] You should also set the values of the first option of each select rather than using price which will probably result in NaN for the total.Leave a comment:
Leave a comment: