Skip to content

Instantly share code, notes, and snippets.

@ZCG-coder
Created September 10, 2024 06:22
Show Gist options
  • Select an option

  • Save ZCG-coder/52fcf26c39e52da451cbeddb155b2871 to your computer and use it in GitHub Desktop.

Select an option

Save ZCG-coder/52fcf26c39e52da451cbeddb155b2871 to your computer and use it in GitHub Desktop.
interest_calc.py
from typing import Union
def separate_number(number: str, sep: str = ""):
result = number[::-1]
insert_idx = []
times = 0
for idx in range(len(number)):
if idx % 3 == 0:
insert_idx.append(idx + times)
times += 1
for i in insert_idx:
result = result[:i] + sep + result[i:]
return result[::-1][:-1] # Return reversed result, with final separator removed
def print_number(number: Union[float, int] = 0, sep: str = ""):
if len(sep) > 1:
print("INVALID SEPARATOR")
result = str(number)
if sep:
parts = result.split(".")
result = separate_number(parts[0], sep)
result += "." + parts[1]
return result
print("-- CPD INTEREST CALCULATOR --")
print("________________________________________________")
print("Principal ", end="")
principal = float(input())
print("Interest Rate (p.a. %) ", end="")
ir = float(input()) / 100
print("Period (years) ", end="")
pd = float(input())
print("Subdivision ")
print("(M)onthly, (Y)early, (H)alf-yearly, (Q)uarterly ", end="")
dv_s = input().lower()
if dv_s == "m":
dv = 12
elif dv_s == "y":
dv = 1
elif dv_s == "h":
dv = 2
elif dv_s == "q":
dv = 4
else:
dv = 0
exit(1)
ir_orig = ir
pd_orig = pd
ir /= dv
pd *= dv
amount = principal * (1 + ir) ** pd
interest = amount - principal
print()
print(f"PERIODS = {pd_orig} * {dv}")
print(f" = {int(pd)}")
print(f"INTEREST RATE PER PERIOD = {ir_orig * 100:.3f}% / {dv}")
print(f" = {ir * 100:.3f}%")
print()
print(
f"AMOUNT = ${print_number(principal, sep=',')} * (1 + {ir * 100:.3f}%) ^ {int(pd)}"
)
print(f" = ${amount:.3f}...")
print(f" = ${amount:.0f} (cor. to nearest dollar)")
print()
print(f"INTEREST = ${amount} - ${print_number(principal, sep=',')}")
print(f" = ${interest:.3f}...")
print(f" = ${interest:.0f} (cor. to nearest dollar)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment