Guest User

Untitled

a guest
Mar 23rd, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. """
  2. Calculate EMI for given Principal, Interest, Tenure
  3.  
  4. Author: T K Sourab<sourabhtk37@gmail.com>
  5. """
  6.  
  7.  
  8. # todo: implement extra payments
  9. class EmiCalc:
  10.  
  11. """ Implements Equated Monthly Instalment (EMI)
  12. """
  13.  
  14. def __init__(self, principal_amt, interest_rt, tenure_period):
  15. self.principal_amt = principal_amt
  16. self.interest_rt = interest_rt / (12 * 100) # monthly interest
  17. self.tenure_period = tenure_period
  18.  
  19. @property
  20. def monthly_emi(self):
  21. """Returns monthly EMI amount to be paid
  22. EMI = (p * r * pow(1 + r, t)) / (pow(1 + r, t) - 1)
  23. """
  24. return (
  25. self.principal_amt * self.interest_rt * pow(1 + self.interest_rt,
  26. self.tenure_period)) / (pow(1 + self.interest_rt,
  27. self.tenure_period) - 1)
  28.  
  29. def print_monthly_breakdown(self):
  30. """Provides breakdown of monthly interest, principal,EMI,
  31. opening and closing balance.
  32. """
  33. begin_bal = self.principal_amt
  34. end_bal = 0
  35. monthly_interest = 0
  36. monthly_principal = 0
  37.  
  38. print("Month \t Beginning Balance \t EMI \t\t Interest Amount"
  39. " \t Principal Amount \t Ending Balance")
  40.  
  41. for month in range(1, self.tenure_period + 1):
  42.  
  43. monthly_interest = self.interest_rt * begin_bal
  44. monthly_principal = self.monthly_emi - monthly_interest
  45. end_bal = begin_bal - monthly_principal
  46.  
  47. # todo: seperate function/class for formatter
  48. format_tuple = (month, begin_bal,
  49. self.monthly_emi,
  50. monthly_interest,
  51. monthly_principal,
  52. abs(end_bal))
  53. formatted_out = """{0} \t {1:.3f} \t\t {2:.3f} \t {3:.3f} \t\t {4:.3f} \t\t {5:.3f}
  54. """.format(*format_tuple)
  55.  
  56. print(formatted_out)
  57.  
  58. begin_bal = end_bal # next month opening balance updation
  59.  
  60.  
  61. if __name__ == '__main__':
  62. loan_user = EmiCalc(25000, 10, 4)
  63. print("The monthly EMI to be paid is %s" % loan_user.monthly_emi)
  64. loan_user.print_monthly_breakdown()
Add Comment
Please, Sign In to add comment