为什么我的抵押贷款计算器显示错误的小数位?

时间:2017-10-27 18:32:53

标签: javascript jquery variables math calculator

我正在研究计算器,到目前为止一直很好,除非它将小数放在错误的位置,从而导致我的舍入函数给出错误的数字。代码和示例如下所示:

$(function() {
  // Create USD currency formatter.
  var formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits: 2,
  });

  function calculateAI() {
    var tax = parseFloat(('2,000').replace(/\$|,/g, ''));
    var hoa = parseFloat(('200').replace(/\$|,/g, ''));
    var cable = parseFloat(('30,000').replace(/\$|,/g, ''));
    var util = parseFloat(('44').replace(/\$|,/g, ''));
    var other = parseFloat(('0').replace(/\$|,/g, ''));
    var price = parseFloat(('200,000').replace(/\$|,/g, ''));
    var years = parseFloat(('30').replace(/\$|,/g, ''));
    var percent = parseFloat(('10').replace(/\$|,/g, ''));
    var yearp = parseFloat(('3.5').replace(/\$|,/g, ''));

    var deposit = (percent / 100) * price;
    var loan = price - deposit;
    var interest = (yearp / 100) / 12;
    var months = years * 12;

    var mtotal = loan * interest * (Math.pow(1 + interest, months)) / (Math.pow(1 + interest, months) - 1) || 0;
    var mortamount = mtotal;

    var ai_total = tax + hoa + cable + util + other + mortamount;

    document.getElementById('1870').innerHTML = formatter.format(ai_total);
    document.getElementById('1870-mort').innerHTML = mortamount;
  }
  calculateAI();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="1870-mort"></div>
<div id="1870" style="margin-top: 3px;"></div>

计算器正在发出 -

808.2804380558849

但它假设是 -

80.82804380558849

所以我可以把它四舍五入到“81”然后继续我的计算。

我做错了什么?

3 个答案:

答案 0 :(得分:4)

下面的代码段中添加了一些控制台日志记录。这输出如下:

tax = 2000, hoa = 200, cable = 30000, util = 44, other = 0, price = 200000, years = 30,
       percent = 10, yearp = 3.5, loan = 180000, deposit = 20000, months = 360, 
       interest = 0.002916666666666667
mtotal = loan * interest * (Math.pow(1 + interest, months))
                         / (Math.pow(1 + interest, months) - 1)
       = 180000 * 0.002916666666666667 * (1.0029166666666667 ^ 360)
                                       / ((1.0029166666666667 ^ 360) - 1)
       = 180000 * 0.002916666666666667 * 2.853287165195746
                                       / 1.8532871651957459
       = 808.2804380558849

将相同的数字插入https://www.desmos.com/scientific会得到相同的结果。这里的Javascript似乎没有任何明显的错误(例如由于舍入错误,数字解析,类型转换等),所以我怀疑是公式本身。

&#13;
&#13;
$(function() {
  // Create USD currency formatter.
  var formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits: 2,
  });

  function calculateAI() {
    var tax = parseFloat(('2,000').replace(/\$|,/g, ''));
    var hoa = parseFloat(('200').replace(/\$|,/g, ''));
    var cable = parseFloat(('30,000').replace(/\$|,/g, ''));
    var util = parseFloat(('44').replace(/\$|,/g, ''));
    var other = parseFloat(('0').replace(/\$|,/g, ''));
    var price = parseFloat(('200,000').replace(/\$|,/g, ''));
    var years = parseFloat(('30').replace(/\$|,/g, ''));
    var percent = parseFloat(('10').replace(/\$|,/g, ''));
    var yearp = parseFloat(('3.5').replace(/\$|,/g, ''));
    var deposit = (percent / 100) * price;
    var loan = price - deposit;
    var interest = (yearp / 100) / 12;
    var months = years * 12;

    var mtotal = loan * interest * (Math.pow(1 + interest, months)) / (Math.pow(1 + interest, months) - 1) || 0;
    console.log('tax = ' + tax + ', hoa = ' + hoa + ', cable = ' + cable + ', util = ' + util + ', other = ' + other + ', price = ' + price + ', years = ' + years + ', percent = ' + percent + ', yearp = ' + yearp + ', loan = ' + loan + ', deposit = ' + deposit + ', months = ' + months + ', interest = ' + interest
       + '\n' + 'mtotal = loan * interest * (Math.pow(1 + interest, months)) / (Math.pow(1 + interest, months) - 1)'
       + '\n' + '       = ' + loan + ' * ' + interest + ' * (' + (1 + interest) + ' ^ ' + months + ') / ((' + (1 + interest) + ' ^ ' + months + ') - 1)'
       + '\n' + '       = ' + loan + ' * ' + interest + ' * ' + (Math.pow(1 + interest, months)) + ' / ' + (Math.pow(1 + interest, months) - 1)
       + '\n' + '       = ' + mtotal);

    var mortamount = mtotal;
    var ai_total = tax + hoa + cable + util + other + mortamount;
  }
  calculateAI();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="1870-mort"></div>
<div id="1870" style="margin-top: 3px;"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:4)

我想通了,这是我在帖子页面上的代码中的拼写错误,使得数字不同。事实上,我的公式确实有效。即使我的问题是因为我的错误,希望人们可以找到我发布的有用的代码。

答案 2 :(得分:2)

让我们快速进行健全检查。我们有一笔180,000的贷款,每月可以偿还30年或(360个月)。忽略任何利息累积,最低月付金额

180,000 / 360 = 500

这表明每月808.28美元的数字是在正确的球场,而80.82美元的数字还不足以偿还贷款。