-
Notifications
You must be signed in to change notification settings - Fork 0
/
25-loancalc
33 lines (25 loc) · 978 Bytes
/
25-loancalc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
# loancalc--Given a principal loan amount, interest rate, and
# duration of loan (years), calculates the per-payment amount
# Formula is M = P * ( J / (1 - (1 + J) ^ -N)),
# where P = principal, J = monthly interest rate, N = duration (months).
#
# Users typically enter P, I (annual interest rate), and L (length, years).
. ../1/library.sh # Start by sourcing the script library.
if [ $# -ne 3 ] ; then
echo "Usage: $0 principal interest loan-duration-years" >&2
exit 1
fi
P=$1 I=$2 L=$3
J="$(scriptbc -p 8 $I / \( 12 \* 100 \) )"
N="$(( $L * 12 ))"
M="$(scriptbc -p 8 $P \* \( $J / \(1 - \(1 + $J\) \^ -$N\) \) )"
# Now a little prettying up of the value:
dollars="$(echo $M | cut -d. -f1)"
cents="$(echo $M | cut -d. -f2 | cut -c1-2)"
cat << EOF
A $L-year loan at $I% interest with a principal amount of $(nicenumber $P 1 )
results in a payment of \$$dollars.$cents each month for the duration of
the loan ($N payments).
EOF
exit 0