Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Javascript (https://www.askmehelpdesk.com/forumdisplay.php?f=452)
-   -   2dp output restriction, and loop calculation required, with form. (https://www.askmehelpdesk.com/showthread.php?t=366724)

  • Jun 19, 2009, 07:45 AM
    banjax
    1 Attachment(s)
    2dp output restriction, and loop calculation required, 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. :confused:

    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:




    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);

     }
    }

    HTML Output Excuse the tables please.
    Code:



       
      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.
     
    Costs
    &nbsp;
     
    &nbsp;


    <strong>Surgeries Per Week </strong><br>
        How many breast surgeries do you perform?
     
      &nbsp;


      <strong>Average Minutes Per Surgery </strong>
     
      &nbsp;


      <strong>Average Cost Per Surgery </strong>
     
      &nbsp;


      <strong>Minutes Saved Per Surgery </strong> 
        How many minutes can you save per surgery if you
    don't<br> have to send the specimen out for imaging?
     
      &nbsp;


      <strong>Cost of Digital Equipment</strong>
      List price quoted by Bioptics sales representative.
     
      &nbsp;


      &nbsp;
      &nbsp;
      &nbsp;
         
     


      Results
      &nbsp;
      &nbsp;
      &nbsp;


      <strong>Cost of Surgeries</strong>
     
      &nbsp;


      <strong>Amount saved per surgery</strong>
     
      &nbsp;


      <strong>Surgeries Until Investment is recovered</strong>
     
      &nbsp;


      <strong>Time Until Investment is recovered</strong>
     
     
     


      Weeks
      Months
      Years


      Savings
      &nbsp;
      &nbsp;
      &nbsp;


      &nbsp;
      <strong>Total Number<br />of Surgeries</strong>
      <strong>Total Cost<br />of Surgeries</strong>
      <strong>Total Minutes<br />Saved</strong>
      <strong>Total Amount<br />Saved</strong>


      <strong>Weekly</strong>
     
     
     
     


      <strong>Monthly</strong>
     
     
     
     


      <strong>Annually</strong>
     
     
     
     


      &nbsp;
      &nbsp;
      &nbsp;
      &nbsp;
      <img src="images/print_btn_up.png" alt="" name="Pictures" width="100" height="33" align="top">

    Please excuse the table. I have also attached an image of the output so you have a better idea.

    Many thanks guys. Cheers,

    DS</br></br></br></br></br></br></br></img>
  • Jun 19, 2009, 08:11 AM
    Perito

    I'm not all that familiar with Javascript, but try this link.

    JavaScript Number Format - Decimal Precision - mredkj.com

    Basically, generate a formatted string with

    var result = num.toFixed(2)

    or you can use toPrecision(2).
  • Jun 19, 2009, 08:29 AM
    banjax

    Thanks Perito for your help. One thing though I don't want to have to click to convert this output to X decimal places, which is what that webpage explains. I would like the output to come out as 2 decimal places once calculate is clicked on. Do you know how I could do that?
  • Jun 19, 2009, 08:45 AM
    Perito

    Like I said, I'm not all that familiar with Javascript so I'm learning along with you.

    I don't see anything on that page that says that you have to click anything. They show an example box that uses the function and you have to click on that to execute it. But, I don't see any reason to think that a click is required in a program.

    If you click on the "Calculate" button, I would think that this would happen:

    (I'm assuming that this puts the value in the box, but I don't know that for sure).

    change
    document.calc.f.value = ab * ac;
    to
    document.calc.f.value = (ab * ac).toFixed(2);
  • Jun 19, 2009, 08:53 AM
    banjax

    Yes, brilliant. After about 5 hours of searching and trying out different various methods, finally one that works. Cheers for your help Perito.

    Now to the loop issue. When I click on this form, all I get is infinity, that is until I click again and it uses the previous result to calculate the next. I know it needs to look, I just don't know how. Any help on this would be much appreciated.
  • Jun 19, 2009, 09:02 AM
    Perito

    Quote:

    Now to the loop issue. When I click on this form, all I get is infinity, that is until I click again and it uses the previous result to calculate the next. I know it needs to look, I just don't know how.
    I'm not sure I understand. I think you mean that you're in an infinite loop. Since I don't understand the dynamics of Javascript, I'm not sure I can help you here. (I mean how lucky can I get in one day?) :D

    Do you have a debugger that you can walk through the code with? It may be as simple as setting a semaphore. Something like this (pseudocode, not Javascript)

    At the top of the subroutine, have something like this:

    if SomeBooleanFlag = False then exit the subroutine.

    When you click on "Calculate", set SomeBooleanFlag to True. After passing through the subroutine once, set SomeBooleanFlag to False again. SomeBooleanFlag should be set to False when the program starts up.

  • All times are GMT -7. The time now is 11:54 AM.