Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying parseFloat, but acts like parseBloat 1

Status
Not open for further replies.

EdwardMartinIII

Technical User
Sep 17, 2002
1,655
US
I should be smarter than this. Really. But this is very weird

I am attempting to tally up a row of text fields. Very simple, right? I use parseFloat on each element.

The code in "Sample.js" is below:
Code:
function BigTally()
  {
    // tally each subtotal first
    document.MainForm.i1total.value=(parseFloat(document.MainForm.i1c1.value)+parseFloat(document.MainForm.i1c2.value)+parseFloat(document.MainForm.i1c3.value));
    // now sum the subtotals to the grand total.
    document.MainForm.GrandTotal.value=parseFloat(document.MainForm.i1total.value);
  }

All it does is add the contents of the three fields. This should be a no-brainer.

The HTML code is here:

Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></meta>
    <title>JavaScript Sample</title>
    <script src=&quot;Sample.js&quot; type=&quot;text/javascript&quot;></script>
  </head>
  <body>
    <form name=&quot;MainForm&quot; action=&quot;&quot;>
      <h3>Tally 'em up!</h3>
      <table border=&quot;1&quot;>
        <tr>
          <td>item 1 cost 1: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i1c1&quot; id=&quot;i1c1&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td>item 1 cost 2: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i1c2&quot; id=&quot;i1c2&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td>item 1 cost 3: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i1c3&quot; id=&quot;i1c3&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td><strong>item 1 Total:</strong> <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i1total&quot; id=&quot;i1total&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
        </tr>
        <tr>
          <td>item 2 cost 1: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i2c1&quot; id=&quot;i2c1&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td>item 2 cost 2: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i2c2&quot; id=&quot;i2c2&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td>item 2 cost 3: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i2c3&quot; id=&quot;i2c3&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td><strong>item 2 Total:</strong> <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i2total&quot; id=&quot;i2total&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
        </tr>
        <tr>
          <td>item 3 cost 1: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i3c1&quot; id=&quot;i3c1&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td>item 3 cost 2: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i3c2&quot; id=&quot;i3c2&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td>item 3 cost 3: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i3c3&quot; id=&quot;i3c3&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td><strong>item 3 Total:</strong> <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i3total&quot; id=&quot;i3total&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
        </tr>
        <tr>
          <td>item 4 cost 1: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i4c1&quot; id=&quot;i4c1&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td>item 4 cost 2: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i4c2&quot; id=&quot;i4c2&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td>item 4 cost 3: <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i4c3&quot; id=&quot;i4c3&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
          <td><strong>item 4 Total:</strong> <input type=&quot;text&quot; value=&quot;0&quot; name=&quot;i4total&quot; id=&quot;i4total&quot; size=&quot;5&quot; onblur=&quot;BigTally();return true;&quot;></input></td>
        </tr>
        <tr>
          <td colspan=&quot;4&quot; align=&quot;right&quot;><strong>Grand Total: </strong><input type=&quot;text&quot; value=&quot;0&quot; name=&quot;GrandTotal&quot; id=&quot;GrandTotal&quot;size=&quot;50&quot;></input></td>
        </tr>
      </table>
    </form>
  </body>
</html>

Now, the trouble occurs when I enter
Item 1 cost 1: 83
Item 1 cost 2 45.67

The item 1 total kicks out 128.67000000000002. The same thing happens when I reverse the arguments (even faulty addition is commutative, evidently). However, if I subtract 1 from either argument (82 instead of 83 or 44.67 instead of 45.67), the answer tallies out fine!

What the holy guacamole's goin' on?

Can anyone tell me what I might be doing wrong here?

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top