Member Avatar for by89
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>&nbsp;</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.

Recommended Answers

All 3 Replies

Math.floor rounds a number downwards to the nearest integer and returns that.

function updatesum() {

document.form1.totalfat.value = Math.floor((document.form1.satfat.value) + (document.form1.monofat.value ) + (document.form1.polyfat.value));

}

~G

Member Avatar for by89

i have tried your method but is doesnt work which return me NaN. You stated that Math.floor will round downwards to the nearest integer but the values from the 3 field are retrieve from database how can it rounddown.

Perhaps the function doesn't know they are integers, use the following:

function updatesum() {

document.form1.totalfat.value = Math.floor(parseInt(document.form1.satfat.value) + parseInt(document.form1.monofat.value ) + parseInt(document.form1.polyfat.value));

}

~G

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.