Skip to content

Instantly share code, notes, and snippets.

@gtors
Created December 14, 2023 10:58
Show Gist options
  • Save gtors/644587cc2d244facd6929adf14be7bda to your computer and use it in GitHub Desktop.
Save gtors/644587cc2d244facd6929adf14be7bda to your computer and use it in GitHub Desktop.
Раcсчет аннуительного платежа
def calculate_monthly_payment(
*,
loan: float | int | str | Decimal,
annual_interest_rate: float | str | Decimal,
years: int | None = None,
months: int | None = None,
):
"""
Calculates the annuity monthly mortgage payment
An example of a loan in the amount of 9M at 11% per annum for 15 years:
>>> calculate_monthly_payment(loan=9_000_000, annual_interest_rate="0.11", years=15)
"""
s = Decimal(loan)
p = Decimal(annual_interest_rate)
n = 12 * years if years else months
return s * p / 12 * (1 + p / 12) ** n / ((1 + p / 12) ** n - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment