SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dunamic CF Form Calculations

    I came across the code below on this site which is working fine, but when I use decimal points, the calculations are wrong.

    Exp: if Field-1=10 and Field-2=5 , then Field-3=15 - good so far.
    but when I change 10 to 10.5, I still get 15 rather than 15.5

    Here is the code
    Code:
    <script language="JavaScript">
    var one=0;
    var two=0;
    var three=0;
    var four=0;
    var five=0;
    
    function myCalc() {
    
    
    one=Math.abs(parseInt(document.FORM.one.value));
    two=Math.abs(parseInt(document.FORM.two.value));
    three=Math.abs(parseInt(document.FORM.three.value));
    four=Math.abs(parseInt(document.FORM.four.value));
    five=Math.abs(parseInt(document.FORM.five.value));
    
    document.FORM.four.value=(one+two);
    document.FORM.five.value=(three+four);
    
    }
    </script>
    
    
    <cfform action="acceptValues.cfm" name="FORM" method="get">
    <table width="45%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="39%">one</td>
    <td width="61%"><cfinput type="text" name="one" onBlur="myCalc()" size="12" maxlength="50"></td>
    </tr>
    <tr>
    <td>two</td>
    <td><cfinput type="text" name="two" onBlur="myCalc()" size="12" maxlength="50"></td>
    </tr>
    <tr>
    <td>three</td>
    <td><cfinput type="text" name="three" onBlur="myCalc()" size="12" maxlength="50"></td>
    </tr>
    <tr>
    <td>four</td>
    <td align="right"><cfinput type="text" name="four" size="12" maxlength="50"></td>
    </tr>
    <tr>
    <td>Total(five)</td>
    <td align="right"><cfinput type="text" name="five" size="12" maxlength="50"></td>
    </tr>
    <tr>
    <td><input name="Save" type="button" value="Submit" /></td>
    <td>&nbsp;</td>
    </tr>
    </table>
    
    </cfform>
    Here is the direct link
    http://www.sitepoint.com/forums/showthread.php?t=401093

    Thank you.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's because you're parsing the inputs as integers; Integers, by definition, cannot have a decimal point (because they are whole numbers).

    The Float datatype handles decimals.

    Therefore, by replacing parseInt with parseFloat, it should be fine.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much Jake for your prompt response. Yes, you suggestion worked great!

    Thanks again!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •