Created
April 6, 2024 01:45
-
-
Save ckampfe/305fbb92c8acc64759bbe2eeb5d90d38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env awk | |
BEGIN { | |
print "principal", "interest_rate", "term", "monthly_payment", "total_paid", "interest_paid" | |
} | |
NR == 1 && NF == 0 { | |
print "use:" | |
print "\tawk -f amortize" | |
print "\t\tprincipal, interest-rate, term (months), [down-payment]" | |
print "\t\tlike: 10000 7.0 48 1000" | |
print "outputs:" | |
print "\tprincipal, interest rate, term, monthly payment, total paid, interest paid" | |
exit 1 | |
} | |
{ | |
principal = $1 | |
annual_interest_rate = $2 | |
months = $3 | |
down_payment = $4 | |
if(down_payment) { | |
principal = principal - $4 | |
} | |
monthly_interest_percentage = annual_interest_rate / 12.0 / 100 | |
num = monthly_interest_percentage * (1 + monthly_interest_percentage) ^ months | |
denom = (1 + monthly_interest_percentage) ^ months - 1 | |
monthly_payment = principal * (num / denom) | |
total_paid = principal * (num / denom) * months | |
interest_paid = total_paid - principal | |
print $1, $2, $3, monthly_payment, total_paid, interest_paid | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment