' DOUBLE Financial Calculator — interest, amortization table
#COMPILE EXE
#DIM ALL

FUNCTION CompoundInterest(principal AS DOUBLE, rate AS DOUBLE, periods AS LONG) AS DOUBLE
    LOCAL i AS LONG
    LOCAL result AS DOUBLE
    result = principal
    FOR i = 1 TO periods
        result = result + result * rate
    NEXT i
    FUNCTION = result
END FUNCTION

SUB AmortizationTable(loan AS DOUBLE, rate AS DOUBLE, months AS LONG)
    LOCAL i AS LONG
    LOCAL payment AS DOUBLE
    LOCAL balance AS DOUBLE
    LOCAL interest AS DOUBLE
    LOCAL principal AS DOUBLE
    LOCAL monthlyRate AS DOUBLE
    LOCAL temp AS DOUBLE
    monthlyRate = rate / 12.0
    temp = 1.0
    FOR i = 1 TO months
        temp = temp * (1.0 + monthlyRate)
    NEXT i
    payment = loan * monthlyRate * temp / (temp - 1.0)
    PRINT "=== AMORTIZATION TABLE ==="
    PRINT "Loan: $" & FORMAT$(loan, "######.00")
    PRINT "Annual Rate: " & FORMAT$(rate * 100, "##.##") & "%"
    PRINT "Monthly Payment: $" & FORMAT$(payment, "######.00")
    PRINT "Term: " & STR$(months) & " months"
    PRINT STRING$(60, 61)
    PRINT "Month  Payment   Interest  Principal  Balance"
    PRINT STRING$(60, 61)
    balance = loan
    FOR i = 1 TO months
        interest = balance * monthlyRate
        principal = payment - interest
        IF principal < 0 THEN principal = 0
        balance = balance - principal
        IF balance < 0 THEN balance = 0
        PRINT FORMAT$(i, "###") & "   " & FORMAT$(payment, "#####.00") & "   " & FORMAT$(interest, "####.00") & "    " & FORMAT$(principal, "#####.00") & "  " & FORMAT$(balance, "######.00")
        IF i MOD 12 = 0 THEN
            INPUT "Press Enter to continue...", temp
        END IF
    NEXT i
    PRINT STRING$(60, 61)
    PRINT "Loan paid off!"
    PRINT
END SUB

SUB SimpleInterest(principal AS DOUBLE, rate AS DOUBLE, years AS LONG)
    LOCAL interest AS DOUBLE
    LOCAL total AS DOUBLE
    interest = principal * rate * years
    total = principal + interest
    PRINT "=== SIMPLE INTEREST ==="
    PRINT "Principal: $" & FORMAT$(principal, "#####.00")
    PRINT "Rate: " & FORMAT$(rate * 100, "##.##") & "%"
    PRINT "Years: " & STR$(years)
    PRINT "Interest: $" & FORMAT$(interest, "#####.00")
    PRINT "Total: $" & FORMAT$(total, "#####.00")
    PRINT
END SUB

SUB SavingsForecast()
    LOCAL deposit AS DOUBLE
    LOCAL rate AS DOUBLE
    LOCAL years AS LONG
    LOCAL i AS LONG
    LOCAL balance AS DOUBLE
    deposit = 500.0
    rate = 0.05
    years = 30
    balance = 0
    PRINT "=== SAVINGS FORECAST ==="
    PRINT "Monthly deposit: $" & FORMAT$(deposit, "#####.00")
    PRINT "Annual rate: " & FORMAT$(rate * 100, "##.##") & "%"
    PRINT "Period: " & STR$(years) & " years"
    PRINT STRING$(50, 61)
    PRINT "Year   Deposits   Interest    Balance"
    PRINT STRING$(50, 61)
    FOR i = 1 TO years
        balance = balance + deposit * 12
        balance = balance + balance * rate
        PRINT FORMAT$(i, "###") & "    " & FORMAT$(deposit * 12 * i, "######.00") & "  " & FORMAT$(balance - deposit * 12 * i, "######.00") & "  " & FORMAT$(balance, "######.00")
    NEXT i
    PRINT STRING$(50, 61)
    PRINT
END SUB

SUB CurrencyPrecisionDemo()
    LOCAL a AS DOUBLE
    LOCAL b AS DOUBLE
    LOCAL c AS DOUBLE
    a = 1.23456789
    b = 9.87654321
    c = a + b
    PRINT "=== DOUBLE PRECISION ==="
    PRINT "a = " & FORMAT$(a, "#.########")
    PRINT "b = " & FORMAT$(b, "#.########")
    PRINT "a + b = " & FORMAT$(c, "#.########")
    PRINT "a * b = " & FORMAT$(a * b, "####.##")
    PRINT "b / a = " & FORMAT$(b / a, "####.##")
    PRINT "SIZEOF(DOUBLE) = " & STR$(SIZEOF(a))
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    LOCAL loanAmt AS DOUBLE
    LOCAL annualRate AS DOUBLE
    LOCAL months AS LONG
    PRINT "==============================="
    PRINT " DOUBLE Financial Calculator"
    PRINT "==============================="
    PRINT
    CALL SimpleInterest(10000.0, 0.065, 5)
    CALL CurrencyPrecisionDemo()
    CALL SavingsForecast()
    PRINT "Amortization Table Calculator"
    INPUT "Loan amount: ", loanAmt
    INPUT "Annual rate (e.g. 0.05 for 5%): ", annualRate
    INPUT "Number of months: ", months
    PRINT
    CALL AmortizationTable(loanAmt, annualRate, months)
    PRINT "Compound interest on $1000 at 5% for 10 years:"
    PRINT "$" & FORMAT$(CompoundInterest(1000.0, 0.05, 10), "######.00")
    PRINT
    PRINT "Finance calculations complete."
    FUNCTION = 0
END FUNCTION
