0

我有一个需要六个输入的表格(还有更多但现在只有这些重要)

原价

原税

新票价

新税

费用

客人人数

  1. 当按下“计算”时,我使用 javascript添加原始票价和原始税金以获得原始总数

  2. 然后我添加新票价和新税来获得新的总数

  3. 现在我将原始总数与新总数进行比较

  4. 如果原始总数大于新总数,则应该使用一种方法来完成我需要的数学运算并将结果设置为显示

我最初使用这些值测试所有内容:

原票价=1000原税=200

新票价 = 800 新税 = 200

费用 = 150 客人人数 = 3

可以在这里看到https://pnrbuilder.com/_popups/exchangeMyPNR_MATH_TEST_2.php (页面仅适用于 chrome) 上面的工作完全符合预期,但是当我将值更改为:

原票价 = 949.83 原税 = 321.18

新票价 = 453.91 新税费 = 143.91

费用 = 150 客人人数 = 3

在这里看到https://pnrbuilder.com/_popups/exchangeMyPNR_MATH_TEST.php (页面仅适用于 chrome) 此测试使用错误的 if 语句来完成其余的数学运算

我不明白为什么会发生这种情况,因为原始总数仍然 > 新总数,因此它应该使用与第一个示例相同的方法。我发出警报,让我确切知道使用哪个 if 语句进行数学运算,显然这里使用了错误的语句,但我不知道为什么。

我知道这很复杂,但有人可以帮我弄清楚我的逻辑哪里出了问题吗?


这是完整的功能:

function addMe(frm) {
    var orBase = Number(frm.box1.value);
    var orTax = Number(frm.box2.value);
    var nwBase = Number(frm.box3.value);
    var nwTax = Number(frm.box4.value);
    var fee = Number(frm.fee.value);
    var gsts = Number(frm.guests.value);


    var fltrd_orBase = orBase * 100;
    var fltrd_orTax = orTax * 100;
    var fltrd_orTtl = fltrd_orBase + fltrd_orTax;
    var orTtl = fltrd_orTtl / 100;
    var final_orTtl = orTtl.toFixed(2)
    frm.result.value = orTtl.toFixed(2)

    var fltrd_nwBase = nwBase * 100;
    var fltrd_nwTax = nwTax * 100;
    var fltrd_nwTtl = fltrd_nwBase + fltrd_nwTax;
    var nwTtl = fltrd_nwTtl / 100;
    var final_nwTtl = nwTtl.toFixed(2)
    frm.result2.value = nwTtl.toFixed(2)

    var e = document.getElementById("residual");
    var selectVal = e.options[e.selectedIndex].value;



    if (final_orTtl <= final_nwTtl) {

        document.getElementById("forfeitTable").style.display="none";
        document.getElementById("MCOtable").style.display="none";       

        var undiff = final_nwTtl - final_orTtl;
        var diff =undiff
        document.getElementById("differenceDisplay").innerHTML=diff.toFixed(2);
        frm.difference.value = diff.toFixed(2);

        var ppCost = diff + fee;
        frm.pptotal.value = ppCost.toFixed(2);
        document.getElementById("ppDisplay").innerHTML=ppCost.toFixed(2);

        var ttlCost = ppCost * gsts;
        frm.totalcost.value = ttlCost.toFixed(2);
        document.getElementById("grandTotalDisplay").innerHTML=ttlCost.toFixed(2);              

        // this is just for testing to show which method was actually used
        if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                    // this is in its own if statement so it doesnt popuop while entering data
                    alert('if 1 was used');
                }       

    }


    else if (final_orTtl > final_nwTtl) {



          if (selectVal == "residualX" || selectVal == "residualN") {

              document.getElementById("MCOtable").style.display="none";
              var diff = final_orTtl - final_nwTtl;

              var displayDiff = diff* -1;
              document.getElementById("differenceDisplay").innerHTML= displayDiff.toFixed(2);
              frm.difference.value = displayDiff.toFixed(2);




              document.getElementById("forfeitInfo").innerHTML = "Beyond the cost above";
              frm.lost.value = diff.toFixed(2);
              document.getElementById("ppForfeitedDisplay").innerHTML = diff.toFixed(2);

              var ttlfForfeited = diff * gsts;
              frm.lostTTL.value = ttlfForfeited.toFixed(2);
              document.getElementById("totalForfeitedDisplay").innerHTML=ttlfForfeited.toFixed(2);



              var ppCost = fee;
              frm.pptotal.value = ppCost.toFixed(2);
              document.getElementById("ppDisplay").innerHTML=ppCost;

              var ttlCost = fee * gsts;
              frm.totalcost.value = ttlCost.toFixed(2);
              document.getElementById("grandTotalDisplay").innerHTML=ttlCost;

                if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                    document.getElementById("forfeitTable").style.display="table";
                }
                // this is just for testing to show which method was actually used 
                if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                    // this is in its own if statement so it doesnt popuop while entering data
                    alert('if 2.1 was used');
                }
          }

          // this is the method that should be used below
          else if (selectVal == "residualA" ) {

              document.getElementById("MCOtable").style.display="none";
              var diff = final_orTtl - final_nwTtl;

              var displayDiff = diff* -1;
              document.getElementById("differenceDisplay").innerHTML= displayDiff.toFixed(2);
              frm.difference.value = diff.toFixed(2);

               if ( diff > fee) {

                    var residual =  diff - fee ;

                    document.getElementById("forfeitInfo").innerHTML = "No additional cost. However,";
                    frm.lost.value = residual.toFixed(2);
                    document.getElementById("ppForfeitedDisplay").innerHTML = residual.toFixed(2);

                    var ttlfForfeited = residual * gsts;
                    frm.lostTTL.value = ttlfForfeited.toFixed(2);
                    document.getElementById("totalForfeitedDisplay").innerHTML=ttlfForfeited.toFixed(2);

                    //document.getElementById("differenceDisplay").innerHTML=0;
                    frm.difference.value = diff;

                    document.getElementById("ppDisplay").innerHTML=0;
                    document.getElementById("grandTotalDisplay").innerHTML=0;

                       if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                          document.getElementById("forfeitTable").style.display="table";
                      }
                      // this is just for testing to show which method was actually used 
                      if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                        // this is in its own if statement so it doesnt popuop while entering data
                        alert('if 2.2 was used');
                      }



                }
              else  {

                    var remBal =  fee - diff ;

                    var ttlcost = remBal * gsts;
                    frm.totalcost.value = ttlcost.toFixed(2);

                    document.getElementById("ppDisplay").innerHTML=remBal.toFixed(2);
                    document.getElementById("grandTotalDisplay").innerHTML=ttlcost.toFixed(2);

                    document.getElementById("forfeitTable").style.display="none";

                      // this is just for testing to show which method was actually used 
                      if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                        // this is in its own if statement so it doesnt popuop while entering data
                        alert('if 2.3 was used');
                      }                  
                 }
          }
          else if (selectVal == "residualM" ) {

              var diff = final_orTtl - final_nwTtl;
              var displayDiff = diff* -1;
              document.getElementById("differenceDisplay").innerHTML= displayDiff.toFixed(2);
              frm.difference.value = displayDiff.toFixed(2);

              if (diff > fee) {   



                  var mco =  diff - fee ;
                  document.getElementById("MCOInfo").innerHTML=mco.toFixed(2);
                  frm.MCOamt.value = mco.toFixed(2);
                  //document.getElementById("differenceDisplay").innerHTML=diff;
                  //frm.difference.value = diff* -1;


                  frm.totalcost.value = 0;

                  document.getElementById("ppDisplay").innerHTML=0;
                  document.getElementById("grandTotalDisplay").innerHTML=0;

                     if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                              document.getElementById("forfeitTable").style.display="none";
                              document.getElementById("MCOtable").style.display="table";
                          }

                                  // this is just for testing to show which method was actually used 
                      if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                        // this is in its own if statement so it doesnt popuop while entering data
                        alert('if 2.4 was used');
                      }
              }
              else {   



                  var remBal =  fee - diff ;

                    var ttlcost = remBal * gsts;
                    frm.totalcost.value = ttlcost.toFixed(2);

                    document.getElementById("ppDisplay").innerHTML=remBal.toFixed(2);
                    document.getElementById("grandTotalDisplay").innerHTML=ttlcost.toFixed(2);

                    document.getElementById("forfeitTable").style.display="none";
                    document.getElementById("MCOtable").style.display="none";
                                          // this is just for testing to show which method was actually used 
                      if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                        // this is in its own if statement so it doesnt popuop while entering data
                        alert('if 2.5 was used');
                      }
              }




          }
    }
4

1 回答 1

0
var nwTtl = fltrd_nwTtl / 100;
var final_orTtl = orTtl.toFixed(2)

var orTtl = fltrd_orTtl / 100;
var final_orTtl = orTtl.toFixed(2)


if (final_orTtl > final_nwTtl )  {

}

这部分似乎是我的问题。出于某种原因,我在这里所做的 if 比较似乎不喜欢'.toFixed(2)' 我在上面所做的比较。我将其更改为:

var nwTtl = fltrd_nwTtl / 100;
var orTtl = fltrd_orTtl / 100;

if (orTtl > nwTtl )  {

}

我仍在测试所有其他功能,但到目前为止这似乎按预期工作。我仍然很想知道为什么我的使用'.toFixed(2)'会导致两个明显高于或低于的数字被评估为相反的结果。并将接受对此问题该部分的任何答案

于 2013-02-23T03:49:23.343 回答