@@ -4,18 +4,28 @@ import { pow } from "./pow.mjs";
4
4
const one = new Decimal128 ( "1" ) ;
5
5
const paymentsPerYear = new Decimal128 ( "12" ) ;
6
6
7
- function calculateMonthlyPayment ( p : string , r : string , y : string ) : Decimal128 {
8
- const principal = new Decimal128 ( p ) ;
9
- const annualInterestRate = new Decimal128 ( r ) ;
10
- const years = new Decimal128 ( y ) ;
7
+ function calculateMonthlyPayment (
8
+ principal : Decimal128 ,
9
+ annualInterestRate : Decimal128 ,
10
+ years : Decimal128
11
+ ) : Decimal128 {
11
12
const monthlyInterestRate = annualInterestRate . divide ( paymentsPerYear ) ;
12
13
const paymentCount = paymentsPerYear . multiply ( years ) ;
13
14
const onePlusInterestRate = monthlyInterestRate . add ( one ) ;
14
- const ratePower = pow ( onePlusInterestRate , Number ( paymentCount ) ) ;
15
-
16
- const x = principal . multiply ( monthlyInterestRate ) ;
17
-
18
- return x . multiply ( ratePower ) . divide ( ratePower . subtract ( one ) ) ;
15
+ const ratePower = pow (
16
+ onePlusInterestRate ,
17
+ Number ( paymentCount . toString ( { numDecimalDigits : 0 } ) )
18
+ ) ;
19
+ return principal
20
+ . multiply ( monthlyInterestRate )
21
+ . multiply ( ratePower )
22
+ . divide ( ratePower . subtract ( one ) ) ;
19
23
}
20
24
21
- console . log ( calculateMonthlyPayment ( "5000000" , "0.05" , "30" ) . toString ( ) ) ;
25
+ console . log (
26
+ calculateMonthlyPayment (
27
+ new Decimal128 ( "5000000" ) ,
28
+ new Decimal128 ( "0.05" ) ,
29
+ new Decimal128 ( "30" )
30
+ ) . toString ( { numDecimalDigits : 2 } )
31
+ ) ;
0 commit comments