Skip to content

How to Calculate Simple Interest in Batch Script

Learn how to implement the simple interest formula (I = P × R × T / 100) in Windows Batch scripting, including precision handling, input validation, and PowerShell alternatives for large calculations.

Simple Interest is the most basic form of interest calculation. Unlike compound interest, it is calculated only on the original principal amount, not on accumulated interest. The formula is: I = P \times R \times T / 100, where P is the principal, R is the annual interest rate (as a percentage), and T is the time in years. This is commonly used for short-term loans, bonds, and simple financial estimations.

In this guide, we will demonstrate how to calculate simple interest using Batch arithmetic.

The Strategy: Direct Formula

Since simple interest does not compound, a single set /a calculation is sufficient. The key is managing the order of operations to preserve as much precision as possible with integer math.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Input Parameters
set /p "principal=Enter Principal amount (in cents, e.g., 100000 = $1000.00): "
set /p "rate=Enter Annual Interest Rate (whole number, e.g., 5): "
set /p "time=Enter Time (years): "

:: 2. Calculate Simple Interest (in cents)
:: I = P * R * T / 100
:: Multiply BEFORE dividing to preserve precision
set /a "interest=principal * rate * time / 100"

:: 3. Calculate Total Amount (in cents)
set /a "total=principal + interest"

:: 4. Format for display (cents to dollars.cents)
call :format_cents !principal! principalFmt
call :format_cents !interest! interestFmt
call :format_cents !total! totalFmt

echo.
echo ==========================================
echo PRINCIPAL:  $!principalFmt!
echo RATE:       !rate!%%
echo TIME:       !time! year(s)
echo.
echo INTEREST:   $!interestFmt!
echo TOTAL:      $!totalFmt!
echo ==========================================
pause
exit /b

:format_cents
:: Converts a value in cents to a dollar.cents string
:: Usage: call :format_cents <value> <outputVar>
set "val=%1"
if !val! LSS 0 (
    set "sign=-"
    set /a "val=0 - val"
) else (
    set "sign="
)

if !val! LSS 10 (
    set "%2=!sign!0.0!val!"
) else if !val! LSS 100 (
    set "%2=!sign!0.!val!"
) else (
    set "dollars=!val:~0,-2!"
    set "cents=!val:~-2!"
    set "%2=!sign!!dollars!.!cents!"
)
exit /b

Why Calculate Simple Interest?

  1. Quick Loan Estimates: Calculating the total cost of a short-term loan or bridge financing for a project.
  2. Invoice Generation: Adding interest charges to overdue invoices based on a flat annual rate.
  3. Educational Tools: Teaching basic financial literacy concepts with a clear, understandable formula.

Important Limitations

PowerShell for Precision

@echo off
setlocal

set /p "P=Enter Principal ($): "
set /p "R=Enter Annual Rate (%%): "
set /p "T=Enter Time (years): "

for /f "usebackq delims=" %%I in (`
    powershell -NoProfile -Command "[math]::Round(%P% * %R% * %T% / 100, 2)"
`) do set "interest=%%I"

for /f "usebackq delims=" %%A in (`
    powershell -NoProfile -Command "[math]::Round(%P% + %P% * %R% * %T% / 100, 2)"
`) do set "total=%%A"

echo.
echo Interest: $%interest%
echo Total:    $%total%
pause

Conclusion

Calculating simple interest is one of the most straightforward financial operations you can perform in Batch. With a single arithmetic line, you turn raw financial parameters into actionable cost estimates. While Batch's integer math limits precision, the "work in cents" approach provides a practical workaround. This simple but essential calculation is a building block for any financial reporting or invoicing tool built within the Windows command-line environment.

Navigation