#!/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). source ../1/library.sh # Start by sourcing the script library. if test $Argc -ne 3 { echo "Usage: $0 principal interest loan-duration-years" > !2 exit 1 } setglobal P = $1, I = $2, L = $3 setglobal J = $[scriptbc -p 8 $I / '(' 12 '*' 100 ')'] setglobal N = "$shExpr(' $L * 12 ')" setglobal M = $[scriptbc -p 8 $P '*' '(' $J / '('1 - '('1 + $J')' '^' -$N')' ')'] # Now a little prettying up of the value: setglobal dollars = $[echo $M | cut -d. -f1] setglobal cents = $[echo $M | cut -d. -f2 | cut -c1-2] cat << """ 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). """ exit 0