除以两个输入并乘以100

时间:2017-10-24 20:39:17

标签: javascript html5 knockout.js

我有两个功能匹配,并且总工作正常。我希望函数total project match将第一个函数除以第二个函数乘以100。最后一个函数不起作用!

到目前为止,这是我的代码:

matchContribution.subscribe(function (newValue) {
          if (newValue != undefined && newValue != '') {
            matchContribution(formatInt(newValue));


        var dataValue = Number(matchContribution().replace(/\,/g, ''));

        if (dataValue > 999999999999.99 || dataValue < 0) {
          matchContribution('');
        }

        if (loading == false) {
          sendCommand('SAVE');
        }
      }

    });

    var totalProjectCost = ko.computed(function () {
      var total = 0;
      var hasUserInput = false;
      if (grantRequest() != '' && grantRequest() != undefined) {
        hasUserInput = true;
        total = total + Number(String(grantRequest()).replace(/\,/g, ''));
      }

      if (matchContribution() != '' && matchContribution() != undefined) {
        hasUserInput = true;
        total = total + Number(String(matchContribution()).replace(/\,/g, ''));
      }



      if (total == 0) {
        if (!hasUserInput)
          return '';
        else
          return formatInt('0');
      }
      else {
        if (loading == false) {
          sendCommand('SAVE');
        }
        return formatInt(total);
      }
    });

    var totalProjectMatch = matchContribution()/totalProjectCost();
    if(totalProjectMatch>=0)
      totalProjectMatch = Math.floor(totalProjectMatch);
    else
      totalProjectMatch = Math.ceil(totalProjectMatch);

1 个答案:

答案 0 :(得分:0)

var totalProjectMatch = ko.computed(function () {
      var total = 0;
      var hasUserInput = false;
      if ((grantRequest() != '' && grantRequest() != undefined) && (matchContribution() != '' && matchContribution() != undefined) && (totalProjectCost() != '' && totalProjectCost() != undefined)) {
        hasUserInput = true;
        total = Number(String(matchContribution()).replace(/\,/g, '')) / Number(String(totalProjectCost()).replace(/\,/g, '')); 
        total = total * 100;
      }


      if (total == 0) {
        if (!hasUserInput)
          return '';
        else
          return formatInt('0');
      }
      else {
        if (loading == false) {
          sendCommand('SAVE');
        }
        return formatNumber(total);
      }
    });

我解决了这个问题!非常感谢!希望能帮助别人。