开发者

Formula returns values slightly below "correct" answer?

开发者 https://www.devze.com 2023-03-04 06:42 出处:网络
Okay, I\'m far from a math whiz. Heck, the fact that I remembered enough high school algebra to cobble together any working formula is a triumph for me. So if you notice an unecessarily long or confus

Okay, I'm far from a math whiz. Heck, the fact that I remembered enough high school algebra to cobble together any working formula is a triumph for me. So if you notice an unecessarily long or confusing formula here, that explains that.

But, as one could reasonably expect, something has gone wrong here.

I'm trying to make a simple calculator that displays the cost savings of buying trip tickets for transit versus paying a cash fare. I'm pretty close, but the calculations from the formulas I'm using are not working right in some cases and I can't figure out why.

In the example below, the Base zone and zone 4 calculate the difference as I expected, while zones 1-3 return answers just a teensy bit below the correct answer (i.e. $105.60000000000014 instead of $106), and Zones 1 and 2 return the same answer though the formula is different.

I've looked at this till I've gone crosseyed. I'm sure the answer is pretty plain, but I can't see it. Anybody?

Thanks for the help.

Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<script language="javascript">

<!-- Begin Trip Tickets Savings Calc script
function  doMath4() {
    var one = parseInt(document.theForm4.elements[0].value);
    var two = parseInt(document.theForm4.elements[1].value);
    var selection = document.getElementsByName("zonett")[0].value;

    if(selection == "z4"){
        var prodZ4tt = (((one  *   two) * 4.25) *12) - (((one  *   two) * 3.75) *12);
        alert("Your yearly savings if you buy Trip Tickets is $"  +  prodZ4tt  +  ".");
    }
    else if(selection == "z3"){
        var prodZ3tt = (((one  *   two) * 3.75) *12) - (((one  *   two) * 3.35) *12);
        alert("Your yearly savings if you buy Trip Tickets is $"  +  prodZ3tt  +  ".");
    }
    else if(selection == "z2"){
        var prodZ2tt = (((one  *   two) * 3) *12) - (((one  *   two) * 2.8) *12);
        alert("Your yearly savings if you buy Trip Tickets is $"  +  prodZ2tt  +  ".");
    }
    else if(selection == "z1"){
        var prodZ1tt = (((one  *   two) * 2.5) *12) - (((one  *   two) * 2.3) *12);
        alert("Your yearly savings if you buy Trip Tickets is $"  +  prodZ1tt  +  ".");
    }
    else if(selection == "Base"){
        var prodBasett = (((one  *   two) * 1.5) *12) - (((one  *   two) * 1.5) *12);
        alert("Your yearly savings if you buy Trip Tickets is $"  +  prodBasett  +  ".");
    }
}

// End Trip Tickets Savings Calc script -->

</script>
</head>

<body>


<form name="theForm4" class="calcform">
<h2>You Do the Math: Commuter Express Trip Tickets Vs. Cash</h2>
<div class="calcform-content">
    <div class="formrow-calc">
      <div class="calcform-col1">
        <p>Days you commute on Commuter Express monthly:</p>
      </div>
      <div class="calcform-col2">
        <input type="text">
      </div>
      <div class="calcform-col3">&nbsp;</div>
    </div>
    <div class="clear"></div>


    <div class="formrow-calc">
      <div class="calcform-col1">
        <p>Daily boardings on Commuter Express Bus:</p>

        <table border="0" cellspacing="0" cellpadding="0" class="fareexampletable">
            <tr>
              <td colspan="2" class="savingsleft"><p class="ifyouride">EXAMPLE:</p></td>
              </tr>
            <tr>
              <td class="savingsleft"><p><strong>Go to work:</strong></p></td>
              <td class="savingsright"><p>1 time</p></td>
            </tr>
            <tr>
              <td class="savingsleft"><p><strong>Come home from work:</strong></p></td>
              <td class="additionline savingsright"><p>+1 time</p></td>
            </tr>
            <tr>
              <td class="savingsleft"><p><strong>Total:</strong></p></td>
              <td class="savingsright"><p>2 times</p></td>
            </tr>
          </table>
      </div>
      <div class="calcform-col2">
        <input type="text">
      </div>
      <div class="calcform-col3">&nbsp;</div>
    </div>
    <div class="clear"></div>


    <div class="formrow-calc savings-zone">
      <div class="calcform-col1">
        <p>Choose Zone:</p>
      </div>
      <div class="calcform-col2">
        <select name="zonett">
          <option value="Base">Base</option>
          <option value="z1">Zone 1</option>
          &l开发者_如何学编程t;option value="z2">Zone 2</option>
          <option value="z3">Zone 3</option>
          <option value="z4">Zone 4</option>
        </select>
      </div>
    </div>



    <div class="formrow-calc">


          <div class="calcform-col4-ce">


 <button type="submit" onclick="doMath4()" class="btn-submit"><div class="btn-submit"><img src="img/btn_savings.png" alt="Show My Yearly Savings" /></div></button> 


    </div>
    </div>
    <div class="clear">


</div>



</div>
</form>

</body>
</html>


Here is the code cleaned up as I discussed in my comment:

function  doMath4() {
    var one = parseInt(document.theForm4.elements[0].value);
    var two = parseInt(document.theForm4.elements[1].value);
    var selection = document.getElementsByName("zonett")[0].value;

    var m12 = one * two * 12;
    var prod = null;

    if(selection == "z4")
    {
        prod = (m12 * 4.25)  - (m12 * 3.75);
    }
    else if(selection == "z3")
    {
        prod = (m12 * 3.75) - (m12 * 3.35);
    }
    else if(selection == "z2")
    {
        prod = (m12 * 3) - (m12 * 2.8);
    }
    else if(selection == "z1")
    {
        prod = (m12 * 2.5) - (m12 * 2.32);
    }
    else if(selection == "Base")
    {
        prod = (m12 * 1.5) - (m12 * 1.5); //This is always 0?
    }

    if(prod != null)
    {
            prod = Math.round(prod);
        alert("Your yearly savings if you buy Trip Tickets is $"  +  prod  +  ".");
    }
}

The simplest solution to your question is included above (and below):

prod = Math.round(prod);

This will round the result (prod) to the nearest whole number, regardless of what it is. So in your example, $105.600000... would be rounded up to $106, which is what you asked for.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号