計算 JavaScript 中的複利
複利公式
複利使用以下公式進行計算 -
CI = P*(1 + R/n) (nt) – P
其中,
- P 為本金。
- R 為年利率。
- t 為存錢或借錢的時間。
- n 為每單位 t 進行利息複利合併的次數,例如,如果利息按月複利合併並且 t 以年為單位,則 n 的值為 12。如果利息按季度複利合併並且 t 以年為單位,則 n 的值為 4。
我們需要編寫一個 Javascript 函式,該函式獲取本金、利率、時間和數字 n,並計算複利。
示例
讓我們編寫此函式的程式碼 -
const principal = 2000;
const time = 5;
const rate = .08;
const n = 12;
const compoundInterest = (p, t, r, n) => {
const amount = p * (Math.pow((1 + (r / n)), (n * t)));
const interest = amount - p;
return interest;
};
console.log(compoundInterest(principal, time, rate, n));輸出
控制檯中的輸出:-
979.6914166032097
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP