Created
September 30, 2025 20:36
-
-
Save jhnlsn/96e7fdd995de5e09b92b22b2daeb8803 to your computer and use it in GitHub Desktop.
interest
This file contains hidden or 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 python3 | |
| from decimal import Decimal, ROUND_HALF_EVEN | |
| def calculate_daily_accrual_monthly_compound(principal, annual_rate, time_years): | |
| """ | |
| Calculate interest with daily accrual and monthly compounding. | |
| Uses banker's rounding (ROUND_HALF_EVEN) for all calculations. | |
| Args: | |
| principal: Starting principal amount | |
| annual_rate: Annual interest rate (percentage) | |
| time_years: Time period in years | |
| Returns: | |
| tuple: (final_balance, total_interest_earned) | |
| """ | |
| # Convert to Decimal for precise calculations | |
| balance = Decimal(str(principal)) | |
| annual_rate_decimal = Decimal(str(annual_rate)) / Decimal('100') | |
| daily_rate = annual_rate_decimal / Decimal('365') | |
| # Calculate total days and months | |
| total_days = int(time_years * 365) | |
| total_months = int(time_years * 12) | |
| accrued_interest = Decimal('0') | |
| for month in range(total_months): | |
| # Determine days in this month (approximate 30.42 days/month average) | |
| days_in_month = 30 if month < total_months - 1 else (total_days - month * 30) | |
| # Accrue interest daily for this month | |
| for _ in range(days_in_month): | |
| daily_interest = (balance * daily_rate).quantize( | |
| Decimal('0.00000001'), rounding=ROUND_HALF_EVEN | |
| ) | |
| accrued_interest += daily_interest | |
| # print accrued interest for the month with 8 decimal places | |
| print(f"Month {month + 1}: Accrued Interest: ${accrued_interest:.8f}") | |
| # Apply accrued interest at end of month (round to 2 decimal places) | |
| balance += accrued_interest.quantize(Decimal('0.01'), rounding=ROUND_HALF_EVEN) | |
| accrued_interest = Decimal('0') | |
| # Print the principal after compounding for the month | |
| print(f"Month {month + 1}: New Balance: ${balance:.2f}") | |
| total_interest = balance - Decimal(str(principal)) | |
| return balance, total_interest | |
| def main(): | |
| print("Interest Calculator") | |
| print("Daily Accrual with Monthly Compounding") | |
| print("-" * 40) | |
| # Get user input | |
| principal = float(input("Enter principal amount: $")) | |
| rate = float(input("Enter annual interest rate (%): ")) | |
| time = float(input("Enter time period (years): ")) | |
| # Calculate with daily accrual and monthly compounding | |
| final_balance, total_interest = calculate_daily_accrual_monthly_compound( | |
| principal, rate, time | |
| ) | |
| print("\n" + "=" * 40) | |
| print("INTEREST CALCULATION") | |
| print("Daily Accrual | Monthly Compounding") | |
| print("Banker's Rounding (Round Half Even)") | |
| print("=" * 40) | |
| print(f"Starting Principal: ${Decimal(str(principal)):,.2f}") | |
| print(f"Interest Earned: ${total_interest:,.2f}") | |
| print(f"Final Balance: ${final_balance:,.2f}") | |
| print(f"\nEffective Rate: {(total_interest / Decimal(str(principal)) / Decimal(str(time)) * 100):.4f}% per year") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment