4

我正在嘗試編寫相當於EXCEL PMT函數。PMT in Javascript

在Java腳本

,公式如下:

function PMT (ir, np, pv, fv) { 
/* 
ir - interest rate per month 
np - number of periods (months) 
pv - present value 
fv - future value (residual value) 
*/ 
pmt = (ir * (pv * Math.pow ((ir+1), np) + fv))/((ir + 1) * (Math.pow ((ir+1), np) -1)); 
return pmt; 
} 

這是偉大的,其中Type = 1(即支付發生在週期的開始)PMT Calcs(計算)

不過,我m試圖編寫類型0場景(即付款發生在期末))。

任何數學嚮導誰可以告訴我如何修改我的公式?

感謝

+0

之前,你寫你的公式,你能真正描述了他們是如何工作的?如果你不能,最好到維基百科,並開始閱讀;) – Blender 2011-03-18 14:43:19

回答

0

我不是一個數學奇才,但一個簡單的谷歌搜索把這個主題了:

http://www.excelforum.com/excel-general/370948-pmt-function-does-anyone-know-the-formula.html

在這裏,他有下面的公式類型= 0:

pmt = ((pv - fv) * ir/(1 - (1 + ir)^-(np))); 

也許這會對你有用:)

+0

嗨,馬丁,我在JS中嘗試過這個公式,但它沒有給出與我的公式相同的輸出。 var pmt =((100000-0)*(7.5/12)/(1-(1+(7.5/12))^ - (48))); var pmv = PMT2((7.5/1200),48,100000,0); – dps123 2011-03-18 15:29:27

+0

@ dps123:在一個你使用ir = 7.5/12,另一個使用7.5/1200,當然他們會給出不同的結果。 – 2011-03-18 15:36:21

+0

是的。我的公式預計速率應該除以12並得出正確的值。此外,我改變只分12,仍然結果不同。 – dps123 2011-03-18 15:40:46

4

@ dps123:當我最近不得不使用一些財務公式來轉換Excel工作簿中的函數時,我遇到了EGM Mathematical Finance class,它試圖模仿Excel函數。如果僅僅看看這些功能是如何工作的,像Excel一樣工作,那麼值得一看。

用法示例:

<?php 
/** 
* Case use of financial class. 
* 
* @version $Id: financial_example.php,v 1.0.5 2004-06-23 09:03:56-05 egarcia Exp $ 
* @author Enrique Garcia M. <[email protected]> 
* @copyright (c) 2002-2004 EGM :: Ingenieria sin fronteras 
* @since  Saturday, November 30, 2002 
**/ 

/*************************************************************************** 
* 
* This program is free software; you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published by 
* the Free Software Foundation; either version 2 of the License, or 
* (at your option) any later version. 
* 
***************************************************************************/ 

include('financial_class.php'); 

echo '<pre>'; 
echo 'FV: ' . $f->FV(1.1, 1/360, 0, -100) . "\n"; 
echo 'PV: ' . $f->PV(1.1, 1/360, 0, -100.206306226) . "\n"; 
echo 'PMT: ' . $f->PMT(1.1, 1/360, -100) . "\n"; 
echo 'PMT: ' . $f->PMT(1.1, 1/360, 0, -100.206306226) . "\n"; 
echo 'NPER: ' . $f->NPER(1.1, 53428.7980679, -100) . "\n"; 
echo 'NPER: ' . $f->NPER(1.1, 0, -100, -100.206306226) . "\n"; 
echo 'FV: ' . $f->FV(0.1, 1/360, 0, -100) . "\n"; 
echo 'PV: ' . $f->PV(0.1, 1/360, 0, -100.026478555) . "\n"; 
echo 'PMT: ' . $f->PMT(0.1, 1/360, -100) . "\n"; 
echo 'PMT: ' . $f->PMT(0.1, 1/360, 0, -100.026478555) . "\n"; 
echo 'NPER: ' . $f->NPER(1.1, 37776.4114948, -100) . "\n"; 
echo 'NPER: ' . $f->NPER(1.1, 0, -100, -100.026478555) . "\n"; 
echo 'EFFECT: ' . $f->EFFECT(0.0525, 4) . "\n"; 
echo 'NOMINAL: ' . $f->NOMINAL(0.053543, 4) . "\n"; 
echo 'NPV: ' . $f->NPV(0.1, array(-10000,3000,4200,6800)) . "\n"; 
echo 'XNPV: ' . $f->XNPV(0.09, array(-10000,2750,4250,3250,2750), array(
    mktime(0,0,0,1,1,2008), 
    mktime(0,0,0,3,1,2008), 
    mktime(0,0,0,10,30,2008), 
    mktime(0,0,0,2,15,2009), 
    mktime(0,0,0,4,1,2009), 
    )) . "\n"; 
echo 'XIRR: ' . $f->XIRR(array(-10000,2750,4250,3250,2750), array(
    mktime(0,0,0,1,1,2008), 
    mktime(0,0,0,3,1,2008), 
    mktime(0,0,0,10,30,2008), 
    mktime(0,0,0,2,15,2009), 
    mktime(0,0,0,4,1,2009), 
    ), 0.1) . "\n"; 
echo 'IRR: ' . $f->IRR(array(-70000,12000,15000,18000,21000)) . "\n"; 
echo 'DISC: ' . $f->DISC(
    mktime(0,0,0,1,25,2007), 
    mktime(0,0,0,6,15,2007), 
    97.975, 
    100, 
    0) . "\n"; 
echo 'DISC: ' . $f->DISC(
    mktime(0,0,0,1,25,2007), 
    mktime(0,0,0,6,15,2009), 
    97.975, 
    100, 
    1) . "\n"; 
echo 'DISC: ' . $f->DISC(
    mktime(0,0,0,1,25,2007), 
    mktime(0,0,0,6,15,2007), 
    97.975, 
    100, 
    2) . "\n"; 
echo 'DISC: ' . $f->DISC(
    mktime(0,0,0,1,25,2007), 
    mktime(0,0,0,6,15,2007), 
    97.975, 
    100, 
    3) . "\n"; 
echo 'DISC: ' . $f->DISC(
    mktime(0,0,0,1,25,2007), 
    mktime(0,0,0,6,15,2007), 
    97.975, 
    100, 
    4) . "\n"; 
echo 'INTRATE: ' . $f->INTRATE(
    mktime(0,0,0,2,15,2008), 
    mktime(0,0,0,5,15,2008), 
    1000000, 
    1014420, 
    2) . "\n"; 
echo 'IPMT: ' . $f->IPMT(0.1/12, 3, 3, 8000) . "\n"; 
echo 'IPMT: ' . $f->IPMT(0.1, 3, 3, 8000) . "\n"; 
echo 'RECEIVED: ' . $f->RECEIVED(
    mktime(0,0,0,2,15,2008), 
    mktime(0,0,0,5,15,2008), 
    1000000, 
    0.0575, 
    2) . "\n"; 
echo 'DOLLARDE: ' . $f->DOLLARDE(1.02, 16) . "\n"; 
echo 'DOLLARDE: ' . $f->DOLLARDE(1.1, 32) . "\n"; 
echo 'DOLLARFR: ' . $f->DOLLARFR(1.125, 16) . "\n"; 
echo 'DOLLARFR: ' . $f->DOLLARFR(1.125, 32) . "\n"; 
echo 'FVSCHEDULE: ' . $f->FVSCHEDULE(1, array(0.09,0.11,0.1)) . "\n"; 
echo 'PPMT: ' . $f->PPMT(0.1/12, 1, 2*12, 2000) . "\n"; 
echo 'PPMT: ' . $f->PPMT(0.08, 10, 10, 200000) . "\n"; 
echo 'RATE: ' . $f->RATE(4*12,-200, 8000) . "\n"; 
echo 'RATE: ' . $f->RATE(4*12,-200, 8000)*12 . "\n"; 
echo 'SYD: ' . $f->SYD(30000, 7500, 10, 10) . "\n"; 
echo 'SLN: ' . $f->SLN(30000, 7500, 10) . "\n"; 
echo 'DDB: ' . $f->DDB(1000000, 100000, 10, 4) . "\n"; 
echo 'DELTA: ' . $f->DELTA(5, 4) . "\n"; 
echo 'DELTA: ' . $f->DELTA(5, 5) . "\n"; 
echo 'PRICEDISC: ' . $f->PRICEDISC(mktime(0,0,0,2,16,2008), mktime(0,0,0,3,1,2008), 0.0525, 100, 2) . "\n"; 
echo 'YIELDDISC: ' . $f->YIELDDISC(mktime(0,0,0,2,16,2008), mktime(0,0,0,3,1,2008), 99.795, 100, 2) . "\n"; 
echo 'COUPNUM: ' . $f->COUPNUM(mktime(0,0,0,1,25,2007), mktime(0,0,0,11,15,2008), 2, 1) . "\n"; 
echo 'COUPDAYBS: ' . $f->COUPDAYBS(mktime(0,0,0,1,25,2007), mktime(0,0,0,11,17,2008), 1, 1) . "\n"; 
echo 'VDB: ' . $f->VDB(2400,300,10*365,0,1) . "\n"; 
echo 'VDB: ' . $f->VDB(2400,300,10*12,0,1) . "\n"; 
echo 'VDB: ' . $f->VDB(2400,300,10,0,1) . "\n"; 
echo 'VDB: ' . $f->VDB(2400,300,10*12,6,18) . "\n"; 
echo 'VDB: ' . $f->VDB(2400,300,10*12,6,18,1.5) . "\n"; 
echo 'VDB: ' . $f->VDB(2400,300,10,0,0.875,1.5) . "\n"; 
echo 'MIRR: ' . $f->MIRR(array(-120000,39000,30000,21000,37000,46000), 0.1, 0.12) . "\n"; 
echo 'MIRR: ' . $f->MIRR(array(-120000,39000,30000,21000), 0.1, 0.12) . "\n"; 
echo 'MIRR: ' . $f->MIRR(array(-120000,39000,30000,21000,37000,46000), 0.1, 0.14) . "\n"; 
echo '</pre>'; 
?> 
+1

非常有用的類,tnx。 – pieSquared 2011-06-06 11:15:35

1

@dps

您將需要修改的地方利益因素更改爲(ir * type + 1)

當它是由於年金的意義開始時的分母期間支付價值1Type將確保利息因子(ir + 1)以及何時它是一個普通的年金意思g期末支付的價值0Type將會減少因子爲1

pmt = (ir * (pv * Math.pow ((ir+1), np) + fv))/((ir * type + 1) * (Math.pow ((ir+1), np) -1)); 

您提出的公式實際上是MS Excel如何計算的貨幣功能,即FVPVPMTNPERRATE 5時間價值。通過重新排列等式並且解決FV,PVPMT容易計算前三個。對於NPERRATE需要其他方法,有些使用二進制搜索算法來找到RATE,但找到RATE的更好和優雅的解決方案將與牛頓拉夫森方法。

1

下面是在Java代碼:

double pmt = ((pv - fv) * ir/(1 - Math.pow((1 + ir), -np)));