Math.round和toFloat以及自定义函数不会舍入到2位小数

时间:2014-07-15 13:28:45

标签: javascript math tofixed

我有抵押贷款计算器,我希望将两个输出值四舍五入到小数点后两位。这是网站http://hawkstonefs.duneroot.com,因为你可以看到它根本没有四舍五入。

下面是我的代码,我尝试过Fllo(2),Math.round和一个自定义函数,你可以在使用中看到“roundToTwo(num)”。

<div class="module">
<script>
(function (window) {

function roundToTwo(num) {    
    return +(Math.round(num + "e+2")  + "e-2");
}

var calculateMortgage = function (formId) {
var
form = document.getElementById(formId),
loan = Math.round(Number(form.loan.value.replace(',',''))),
interest_rate = form.interest_rate.value.replace(",","."),
duration, quote, total;

// refresh sanitised values
form.loan.value = loan;
form.interest_rate.value = interest_rate;

// do field validation
if (form.loan.value <= 0) {
  alert('Loan amount is required.');
  form.loan.focus();
} else if (form.duration.value == "") {
  alert('Duration is required.');
  form.duration.focus();
} else if (form.interest_rate.value == "") {
  alert('Interest rate is required.');
  form.interest_rate.focus();
} else {
if (form.duration_units.value == 1) { // Duration in years
  duration = (form.duration.value*12); // in months
} else { // Duration in months
  duration = form.duration.value; // in months
}

interest_rate = (interest_rate/12); // monthly
quote = (loan*interest_rate)/(100*(1-Math.pow((1+(interest_rate/100)), -duration)));
total = (quote * duration);

// if browser supports toFixed() method
if (Number.toFixed) {
  quote = quote.toFixed(2);
  total = total.toFixed(2);
}

form.quote.value = roundToTwo(quote); // monthly
if (form.total) {
  form.total.value = roundToTwo(total); // total
}
}
};

window.mortgageCalc = {
  calculate: calculateMortgage
};

})(window);
</script>

  <form id="mortgage_calc_form">

  <p>
  <input type="text" name="loan" id="loan" size="12" maxlength="12" placeholder="Loan amount" value="" class="inputbox" />
  </p>

  <p>
  <input type="text" name="duration" placeholder="Duration (years)" id="duration" size="2" maxlength="2" value="" class="inputbox" />
  <input type="hidden" name="duration_units" value="1" />
  </p>

  <p>
  <input type="text" name="interest_rate" id="interest_rate" placeholder="Interest rate (3.5%)" size="3" class="inputbox" />
  </p>

  <p>
  <input placeholder="Monthly repayments(GBP)" type="text" name="quote" id="quote"  size="12" class="inputbox" readonly/>
  </p>

  <p>
  <input placeholder="Total to be re-paid(GBP)" type="text" name="total" id="total"
  size="12" class="inputbox" readonly />
  </p>

  <p>
  <input class="mtBtn"  type="reset" name="reset" value="Reset" />
  <input class="mtBtn" type="submit" name="submit" value="Calculate"     onClick="mortgageCalc.calculate('mortgage_calc_form'); return false;" />
  </p>

 </form>
</div>

它必须是我网站上的内容,因为这个js小提琴作品http://jsfiddle.net/ekhqa/

我正在使用Wordpress,此代码在sidebar.php文件中进行了硬编码。

由于 RIC

1 个答案:

答案 0 :(得分:0)

如果您上传此插件:http://mathjs.org/

然后您可以将其更改为:

return math.round(num,2);

这会将数字四舍五入到小数点后两位。

否则你可以做这个人在这个问题中所做的事情Round to at most 2 decimal places (only if necessary)