简体   繁体   English

使用 JavaScript 进行财务计算(有什么问题?)

[英]Financial calculation with JavaScript (What´s wrong?)

I'm trying to make a financial calculation, but there's something wrong.我正在尝试进行财务计算,但出了点问题。

JS Code: JS代码:

function count (){

var coda = 1.500;
var codb = 15;
var codc = 0.06;
var codx = 1;

var result = (codc/codx(codx-((codx+codc)*-codb)))*coda;

alert(result);


}

Message: undefined消息:未定义

In this line var result = (codc/codx(codx-((codx+codc)*-codb)))*coda;在这一行var result = (codc/codx(codx-((codx+codc)*-codb)))*coda;

You try to execute the 2nd codx as a function ( codx() ).您尝试将第二个codx作为函数( codx() )执行。 I guess you miss an operand there.我猜你错过了那里的一个操作数。

Try for example: var result = (codc/codx / (codx-((codx+codc)*-codb)))*coda;尝试例如: var result = (codc/codx / (codx-((codx+codc)*-codb)))*coda;

You are missing a * operator, so the compiler tries to call codx as a function.您缺少*运算符,因此编译器尝试将codx作为函数调用。

To fix your computation, add the * operator as follow:要修复您的计算,请添加*运算符,如下所示:

function count (){

    var coda = 1.500;
    var codb = 15;
    var codc = 0.06;
    var codx = 1;

    var result = (codc/codx * (codx - ((codx+codc)*-codb)))*coda;
    //                      ^

    alert(result);
}

Missing * symbol?缺少 * 符号? codx is being used as a fuction as a result.因此,codx 被用作函数。

 var coda = 1.500; var codb = 15; var codc = 0.06; var codx = 1; var result = (codc/codx*(codx-((codx+codc)*-codb)))*coda; alert(result);

Slightly off-topic but you should never use floating point arithmetic in financial calculations.有点跑题,但你永远不应该在金融计算中使用浮点算法。 Instead calculate by integer cents and then format for viewing as you like.而是按整数美分计算,然后根据需要设置格式以供查看。

In this case,在这种情况下,

It is better to split the mathematical calculation.最好拆分数学计算。

Example:例子:

function count (){

var coda = 1.500;
var codb = 15;
var codc = 0.06;
var codx = 1;

var res1 = ((codx+codc)*-codb);
var res2 = codx-res1;
var result = (codc/codx*res2)*coda;

alert(result);

}
var count = function () {
 var coda = 1.5; var codb = 15; var codc = 0.06; var codx = 1;
 var result = (codc/codx ** here ** (codx-((codx+codc)* - codb))) *  coda;    
 console.log(result);
}  

PS you have to put something after codx it's seen by javascript as an undefined function. PS 你必须在codx之后放一些东西,它被 javascript 视为未定义的函数。

If this relates to paying down a loan at interest i per payment period, then you get that after n payments at a rate of r the reduced principal is如果这涉及在每个还款期以i的利息偿还贷款,那么在以r的利率支付n后,减少的本金为

p*(1+i)^n-r*(1+i)^n-r*(1+i)^(n-1)-...-r*(1+i) 
=
p*(1+i)^n - (1+i)*((1+i)^n-1)/i*r

If that is to be zero, loan repaid, then the necessary rate computes as如果为零,则偿还贷款,则必要利率计算为

r = i/((1+i)*(1-(1+i)^(-n))) * p

which is in some aspects similar, in other fundamental aspects different from your formula.这在某些方面相似,在其他基本方面与您的公式不同。

 var p = 1.500; var n = 15; var i = 0.06; var x = 1+i; var result = i/( x*( 1-Math.pow(x,-n) ) )*p; alert(result);

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

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