Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    2 decimal place output restriction, and loop calculation needed, with form.

    Hello everyone.

    I have a form that I have developed and am stuck. The outputs that are displayed should be in 2 decimal places. I have searched for ways to do this, but to no avail have they worked for my piece of code. Please have a look and if you could, suggest a way of making this output work for me as needed.

    The form is used to calculate savings and is working as expected (giving me the correct results from the calculated inputs), but only fully after clicking 4 times. I guess this is because the form needs the previous outputs to go ahead with the next calculation, could you also suggest a method that the calculation could loop and complete all calculations with only one click? I'm sure there is a way, I just have no clue how to.

    Code

    JavaScript
    Code:
    </script>
    
    <script language="JavaScript">
    
    function showpay() {
     if ((document.calc.a.value == null || document.calc.a.value.length == 0) ||
         (document.calc.b.value == null || document.calc.b.length == 0) ||
         (document.calc.c.value == null || document.calc.c.value.length == 0) ||
         (document.calc.d.value == null || document.calc.d.value.length == 0) ||
         (document.calc.e.value == null || document.calc.e.value.length == 0))
    	  
     { alert("Please enter all values");
     }
     else
     {
     var aa = document.calc.a.value;
     var ab = document.calc.b.value;
     var ac = document.calc.c.value;
     var ad = document.calc.d.value;
     var ae = document.calc.e.value;
    
     var af = document.calc.f.value;
     var ag = document.calc.g.value;
     var ah = document.calc.h.value;
      
     var ai = document.calc.i.value;
     var aj = document.calc.j.value;
     var ak = document.calc.k.value;
    
     var al = document.calc.l.value;
     var am = document.calc.m.value;
     var an = document.calc.n.value;
     var ao = document.calc.o.value;
     
     var ap = document.calc.p.value;
     var aq = document.calc.q.value;
     var ar = document.calc.r.value;
     var as = document.calc.s.value;
     
     var at = document.calc.t.value;
     var au = document.calc.u.value;
     var av = document.calc.v.value;
     var aw = document.calc.w.value;
    
    // calculations
    
     document.calc.f.value = ab * ac;
     document.calc.g.value = ac * ad;
     document.calc.h.value = ae / ag;
     
     document.calc.i.value = ah / 4.3;
     document.calc.j.value = ai / 4.3;
     document.calc.k.value = ai / 52;
     
     document.calc.l.value = aa * 1;
     document.calc.m.value = "$" + aa * af;
     document.calc.n.value = ad * af;
     document.calc.o.value = "$" + aa * ag;
     
     document.calc.p.value = aa * 4.3;
     document.calc.q.value = "$" + af * ap; 
     document.calc.r.value = ad * ap;
     document.calc.s.value = "$" + ag * ap;
     
     document.calc.t.value = ap * 12; 
     document.calc.u.value = "$" + af * at;
     document.calc.v.value = ad * at;
     document.calc.w.value = "$" + (ag * at);
    
     }
    }
    
    </script>
    HTML Output Excuse the tables please.
    Code:
    <form name=calc method=POST>
    <table class="caltable" width=84% border=0>
    <tr>
      <th height="81" colspan="2">&nbsp;</th>
      <td colspan="3"><div align="left">Enter your data in the white fields to work out cost savings <br /> of a Bioptics Specimen Radiography system, or test different scenarios by varying the inputs.</div></td>
      </tr>
    <tr><td height="42" colspan="2" align=left><h3>Costs</h3>
    <th width=18%>&nbsp;</th>
      <th width=18%></th>
    <th width=18%>&nbsp;</th>
    </tr>
    
    <tr height="20%"><td height="50" colspan="3" valign="top"><strong>Surgeries Per Week </strong><br>
         How many breast surgeries do you perform?</td>
      <td align=right valign="top"><input type=text name=a size=13></td>
      <td align=right valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td height="30" colspan="3" valign="top"><strong>Average Minutes Per Surgery </strong></td>
      <td
    align=right valign="top"><input type=text name=b size=13></td>
      <td
    align=right valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td height="30" colspan="3" valign="top"><strong>Average Cost Per Surgery </strong></td>
      <td  align=right valign="top"><input name=c type=text size=13></td>
      <td  align=right valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td height="66" colspan="3" valign="top"><strong>Minutes Saved Per Surgery </strong> <br/ > 
        How many minutes can you save per surgery if you 
    don't<br> have to send the specimen out for imaging?</td>
      <td 
    align=right valign="top"> <input type=text name=d size=13></td>
      <td 
    align=right valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td height="48" colspan="3" valign="top"><strong>Cost of Digital Equipment</strong> <br/ >
      List price quoted by Bioptics sales representative.</td>
      <td 
      align=right valign="top"> <input name=e type=text size=13></td>
      <td 
      align=right valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td width="25%" height="35" align=center>&nbsp;</td>
      <td width="21%" height="35" align=center>&nbsp;</td>
      <td height="35" align=center>&nbsp;</td>
      <td height="35" align=center valign="top"><input type="button" onClick='showpay()' value=Calculate>    </td>
      <td height="35" align=center valign="top"><input name="reset" type=reset value=Reset></td>
    </tr>
    <tr>
      <td height="33" colspan="2" align=left valign="top"><h3>Results</h3></td>
      <td align=right valign="top">&nbsp;</td>
      <td align=right valign="top">&nbsp;</td>
      <td align=right valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td height="30" colspan="3" valign="top"><strong>Cost of Surgeries</strong> </td>
      <td align=right valign="top"> <input type=text name=f size=13></td>
      <td align=right>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" colspan="3" valign="top"><strong>Amount saved per surgery</strong> </td>
      <td align=right valign="top"><input type=text name=g size=13></td>
      <td align=right>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" colspan="3" valign="top"><strong>Surgeries Until Investment is recovered</strong> </td>
      <td align=right valign="top"><input type=text name=h size=13></td>
      <td align=right>&nbsp;</td>
    </tr>
    <tr>
      <td height="45" colspan="2" rowspan="2" valign="top"><strong>Time Until Investment is recovered</strong></td>
      <td align=right valign="top"><input type=text name=i size=13></td>
      <td align=right valign="top"><input type=text name=j size=13></td>
      <td align=right valign="top"><input type=text name=k size=13></td>
    </tr>
    <tr>
      <td align=center valign="top">Weeks</td>
      <td align=center valign="top">Months</td>
      <td align=center valign="top">Years</td>
    </tr>
    <tr>
      <td height="32" colspan="2" align=left><h3>Savings</h3></td>
      <td align=right valign="top">&nbsp;</td>
      <td align=right valign="top">&nbsp;</td>
      <td align=right valign="top">&nbsp;</td>
    </tr>
    <tr>
      <td height="35" align=center>&nbsp;</td>
      <td height="35" align=center><strong>Total Number<br />of Surgeries</strong></td>
      <td height="35" align=center><strong>Total Cost<br />of Surgeries</strong></td>
      <td height="35" align=center><strong>Total Minutes<br />Saved</strong></td>
      <td height="35" align=center><strong>Total Amount<br />Saved</strong></td>
    </tr>
    <tr>
      <td width="25%" height="30" valign="top"><strong>Weekly</strong></td>
      <td align=right valign="top"><input type=text name=l size=13></td>
      <td align=right valign="top"><input type=text name=m size=13></td>
      <td align=right valign="top"><input type=text name=n size=13></td>
      <td align=right valign="top"><input type=text name=o size=13></td>
    </tr>
    <tr>
      <td width="25%" height="30" valign="top"><strong>Monthly</strong></td>
      <td align=right valign="top"><input type=text name=p size=13></td>
      <td align=right valign="top"><input type=text name=q size=13></td>
      <td align=right valign="top"><input type=text name=r size=13></td>
      <td align=right valign="top"><input type=text name=s size=13></td>
    </tr>
    <tr>
      <td width="25%" height="33" valign="top"><strong>Annually</strong></td>
      <td align=right valign="top"><input type=text name=t size=13></td>
      <td align=right valign="top"><input type=text name=u size=13></td>
      <td align=right valign="top"><input type=text name=v size=13></td>
      <td align=right valign="top"><input type=text name=w size=13></td>
    </tr>
    <tr>
      <td height="30" align=center>&nbsp;</td>
      <td align=right valign="top">&nbsp;</td>
      <td align=right valign="top">&nbsp;</td>
      <td align=right valign="top">&nbsp;</td>
      <td height="45" align=center valign="top"><img src="images/print_btn_up.png" alt="" name="Pictures" width="100" height="33" align="top"></td>
    </tr>
    </table>
    </form>
    Many thanks guys. Cheers,

    DS

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    To show a value with only 2 decimal places example:-

    document.calc.o.value = "$" + (aa * ag).toFixed(2);

    The reason why your form takes four clicks to calculate is that that the operations are not performed in the correct order.

    The five input value are named a-e. But then you try to resolve

    var af = document.calc.f.value;
    var ag = document.calc.g.value;
    var ah = document.calc.h.value;

    before the values of f, g and h have been calculated.

    You need to re-work the whole thing something like this:-

    var af = ab *ac;
    document.calc.f.value = af;
    var ag = ac * ad;
    document.calc.g.value = ag;
    var ah = ae/ag;
    document.calc.h.value = ah;

    In other words, you must evaluate each variable before trying to use it in a subsequent statement.

    <SCRIPT language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.


    "Insanity is doing the same thing over and over again, but expecting different results." - Rita Mae Brown
    Last edited by Philip M; 06-19-2009 at 06:35 PM.


  •  

    Tags for this Thread

    Posting Permissions

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