added rounding - lines up with reference values

This commit is contained in:
- 2024-02-08 22:05:35 -08:00
parent 01523d97f1
commit 7706949f76

View File

@ -7,6 +7,7 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, one_time
# Calculate the fixed monthly payment using the amortization formula # Calculate the fixed monthly payment using the amortization formula
monthly_payment = (principal * monthly_interest_rate) / (1 - (1 + monthly_interest_rate) ** -num_payments) monthly_payment = (principal * monthly_interest_rate) / (1 - (1 + monthly_interest_rate) ** -num_payments)
monthly_payment = round(monthly_payment, 2)
# Initialize variables # Initialize variables
remaining_balance = principal remaining_balance = principal
@ -14,10 +15,10 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, one_time
for payment_number in range(1, num_payments + 1): for payment_number in range(1, num_payments + 1):
# Calculate interest for the current period # Calculate interest for the current period
interest_payment = remaining_balance * monthly_interest_rate interest_payment = round(remaining_balance * monthly_interest_rate, 2)
# Calculate principal payment # Calculate principal payment
principal_payment = monthly_payment - interest_payment principal_payment = round(monthly_payment - interest_payment, 2)
# Apply one-time payment if provided # Apply one-time payment if provided
if one_time_payment and payment_number == one_time_payment['payment_number']: if one_time_payment and payment_number == one_time_payment['payment_number']:
@ -32,17 +33,18 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, one_time
'Payment Number': payment_number, 'Payment Number': payment_number,
'Payment Amount': monthly_payment, 'Payment Amount': monthly_payment,
'Interest Payment': interest_payment, 'Interest Payment': interest_payment,
'Principal Payment': principal_payment, 'Principal Payment': round(principal_payment,2),
'Remaining Balance': remaining_balance 'Remaining Balance': round(remaining_balance,2)
} }
amortization_schedule.append(payment_details) amortization_schedule.append(payment_details)
return amortization_schedule return amortization_schedule
if __name__ == "__main__":
# Example usage # Example usage
principal = 100000 principal = 100000
interest_rate = 5.0 interest_rate = 5.0
loan_term = 30 loan_term = 3
one_time_payment = { one_time_payment = {
'payment_number': 13, 'payment_number': 13,
@ -53,4 +55,6 @@ schedule = generate_amortization_schedule(principal, interest_rate, loan_term, o
# Display the amortization schedule # Display the amortization schedule
for payment in schedule: for payment in schedule:
print(payment) # print(payment)
attrs = [payment[key] for key in payment]
print(attrs)