function updatesum() {
document.form1.totalfat.value = (document.form1.satfat.value -0) + (document.form1.monofat.value -0) + (document.form1.polyfat.value -0);
}
<body onload="updatesum()">
<form name="form1" method="post" action="./submitFood">
<table width="520" border="0">
<%
int count=0;
if(request.getAttribute("foodList")!=null)
{
ArrayList al = (ArrayList)request.getAttribute("foodList");
Iterator itr = al.iterator();
while(itr.hasNext())
{
if((count%2)==0)
{
}
else
{
}
count++;
ArrayList foodList = (ArrayList)itr.next();
%>
<tr>
<th scope="col"><div align="left">Saturated Fat</div></th>
<th scope="col"><div align="left">Monosaturated Fat</div></th>
<th scope="col"><div align="left">Polysaturated Fat</div></th>
</tr>
<tr>
<td> <input type="text" name="satfat" id="satfat" value="<%=foodList.get(1)%>" /> </td>
<td><input type="text" name="monofat" id="monofat" value="<%=foodList.get(2)%>" /></td>
<td><input type="text" name="polyfat" id="polyfat" value="<%=foodList.get(3)%>"/></td>
</tr>
<tr>
<th scope="col"><div align="left">Total Fat</div></th>
</tr>
<tr>
<td><input type="text" name="totalfat" id="totalfat" onload="updatesum()" /></td>
</tr>
<%
}
}
%>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</table>
<p> </p>
<p>
<label>
<input type="submit" name="submit" id="submit" value="Submit">
</label>
</p>
</form>
I like to sum the values from the "satfat", "monofat" and "polyfat" text field into "totalfat" textfield. The values in those 3 text field is retrieve from database. However, when it add up the 3 values, it will not be in decimal place instaead it will be in 30.10000000000000000000000002. How do i solve it and how to go about editing the code.