Compound Interest is interest calculated on the initial principal and on the accumulated interest from previous periods. It is the basis of savings accounts, loans, and investment growth. The formula is: A = P * (1 + r/n)^(nt), where P is the principal, r is the annual rate, n is the compounding frequency, and t is the number of years. In Batch, we simulate this with a loop since we cannot use floating-point math.
In this guide, we will demonstrate how to approximate compound interest using an iterative loop with scaled integers.
The Strategy: The Iterative Growth Loop
Instead of computing the formula directly (which requires decimals and exponents), we can simulate each compounding period:
- For each period, calculate the interest earned.
- Add the interest to the principal.
- Repeat for all periods.
Implementation Script
@echo off
setlocal enabledelayedexpansion
:: 1. Input Parameters (use millicents for precision)
:: Principal: $1000.00 = 100000 millicents
:: Rate: 5%% annual = 5
:: Compounding periods per year: 12 (monthly)
:: Time: 3 years
set "principal=100000"
set "ratePercent=5"
set "periodsPerYear=12"
set "years=3"
set /a "totalPeriods=periodsPerYear * years"
set "balance=!principal!"
:: Display initial values
set "initDollars=!principal:~0,-3!"
set "initCents=!principal:~-3!"
if not defined initDollars set "initDollars=0"
:: Convert millicents to display cents (truncate last digit)
set "initCents=!initCents:~0,2!"
echo Initial: $!initDollars!.!initCents!
echo Rate: !ratePercent!%% compounded !periodsPerYear!x/year for !years! years
:: 2. The Growth Loop
:: Interest per period = balance * rate / (100 * periodsPerYear)
:: Using millicents gives us 3 digits of sub-dollar precision
for /L %%i in (1,1,!totalPeriods!) do (
set /a "interest=!balance! * ratePercent / (100 * periodsPerYear)"
set /a "balance+=interest"
)
:: 3. Display Final Balance
:: Balance is in millicents - extract dollars and cents
set "dollars=!balance:~0,-3!"
set "cents=!balance:~-3,2!"
if not defined dollars set "dollars=0"
echo.
echo ==========================================
echo FINAL BALANCE: $!dollars!.!cents!
echo ==========================================
:: 4. Calculate total interest earned
set /a "earned=balance - principal"
set "earnedDollars=!earned:~0,-3!"
set "earnedCents=!earned:~-3,2!"
if not defined earnedDollars set "earnedDollars=0"
echo INTEREST EARNED: $!earnedDollars!.!earnedCents!
echo ==========================================
pauseWhy Calculate Compound Interest in Batch?
- Financial Planning: Estimating how much a savings account will grow over a specific period.
- Loan Analysis: Understanding how much total interest will be paid on a loan over its lifetime.
- Budget Scripts: If you build a financial planning tool in Batch, compound interest is a core calculation.
Important Limitations
PowerShell for Precision
@echo off
setlocal
set "P=1000"
set "r=0.05"
set "n=12"
set "t=3"
for /f "usebackq delims=" %%R in (`
powershell -NoProfile -Command "[math]::Round(%P% * [math]::Pow((1 + %r%/%n%), %n%*%t%), 2)"
`) do set "result=%%R"
echo Final balance: $%result%
pauseConclusion
Calculating compound interest in Batch demonstrates the power of iterative simulation for financial math. By working in millicents and looping through each compounding period, you build a practical tool for estimating growth and costs. This technique is essential for building financial calculators and budget analysis tools within the Windows command-line environment.