简体   繁体   English

Excel公式到JavaScript功能

[英]excel formula to javascript function

This is the excel formula that I have 这是我拥有的Excel公式

fx = ROUNDUP(C17/((1-(1/(1+(C19/12))^(C18*12)))/(C19/12)),0)

excel公式

This is the result should be : 结果是: 结果

This is the javascript function that I wrote: 这是我写的javascript函数:

function refreshRenoLoanCalculator() {

    var amt = parseInt($('.calc-renoloan .principal-amount').val().replace(/,/g, ''), 10)
    console.log(amt); //10 000
    var rate = $('.calc-renoloan .loan-rate').val();
    console.log(rate); //0.0444
    var tenure = $('.calc-renoloan .loan-tenure').val();
    console.log(tenure); // 5

    var a = rate / 100. / 12.;
    console.log(a); 
    var b = 1. + a;
    b = Math.pow(b, (tenure * 12)) - 1.;
    console.log(b); 
    var FC = a / b + a;
    FC = FC.toFixed(10);
    console.log(FC);
    var RP = amt * FC;
    console.log(RP);




    toolsSetCalculatedValue('.calc-renoloan .calc-result-installment', Math.ceil(RP).toLocaleString().split('.')[0])
    $('.calc-renoloan .results-container').slideDown();

}

At the moment the result that I'm getting from the excel formula and the javascript function is not the same. 目前,我从excel公式和javascript函数得到的结果是不相同的。 Any help would be greatly appreciated. 任何帮助将不胜感激。

In JS the equivalent calculation would be this: 在JS中,等效的计算方式如下:

 var C17 = 10000; var C18 = 5; var C19 = 4.44; var interest = C19 / 1200; var foo = Math.ceil(C17 * interest / (1 - (Math.pow(1/(1 + interest), C18 * 12)))); console.log(foo); 

The main difference is the multiplication of the interest rate ( * 1200 as opposed to * 12 ) as Excel stores percentage values as floating points between 0 and 1 . 主要区别是利率的乘积( * 1200 ,而不是* 12 ),因为Excel将百分比值存储为介于01之间的浮点数。

If you'd prefer to calculate in the same manner as Excel, then you need to convert the input rate to this: 如果您希望以与Excel相同的方式进行计算,则需要将输入速率转换为此:

 var C17 = 10000; var C18 = 5; var C19 = 0.0444; // Note the difference here var interest = C19 / 12; // and here var foo = Math.ceil(C17 * interest / (1 - (Math.pow(1 / (1 + interest), C18 * 12)))); console.log(foo); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM