How to Calculate Percentage of a Number in Batch Script
Calculating percentages is a fundamental task, but it presents a challenge in batch scripting. The SET /A
command, which is the only native tool for arithmetic, does not support floating-point numbers (decimals). This means a standard formula like 50 / 200 = 0.25
is impossible to calculate directly. SET /A
would simply return 0
.
To work around this, we must use integer math creatively by changing the order of operations to preserve our data's precision.
The Key Technique: Multiply Before You Divide
The core principle for all percentage calculations in batch is to perform the multiplication before the division.
- Wrong:
(50 / 200) * 100
--> In integer math, this becomes0 * 100 = 0
. Incorrect. - Correct:
(50 * 100) / 200
--> In integer math, this becomes5000 / 200 = 25
. Correct!
By scaling our number up first, we avoid the fractional part being truncated by the integer-only division.
Example 1: What is X% of a Number?
This is the most common calculation: finding a percentage of a given value.
Goal: Calculate 25% of 200.
Formula: (TotalValue * Percentage) / 100
@echo off
set "TotalValue=200"
set "Percentage=25"
set /a "Result = (TotalValue * Percentage) / 100"
echo %Percentage%%% of %TotalValue% is: %Result%
Output:
25% of 200 is: 50
The %%
is used to display a literal percent sign in a batch script.
Example 2: What Percentage is A of B?
Here we want to find the percentage that one number represents of another.
Goal: Find what percentage 50 is of 200.
Formula: (Part * 100) / Whole
@echo off
set "Part=50"
set "Whole=200"
set /a "Result = (Part * 100) / Whole"
echo %Part% is %Result%%% of %Whole%.
Output:
50 is 25% of 200.
Creating a Reusable Percentage Function
For any real script, creating a reusable function is far more practical. The function below can handle the most common percentage operations in one place.
How It Works:
The :percent
function takes a mode
as its first argument to determine what to do:
- OF: Calculates
X% of Y
. (e.g.,25 OF 200
) - ISOF: Calculates
what % X is of Y
. (e.g.,50 ISOF 200
) - INCREASE: Increases a number by a percentage. (e.g.,
200 INCREASE 25
) - DECREASE: Decreases a number by a percentage. (e.g.,
200 DECREASE 25
)
@echo off
SETLOCAL
ECHO --- Reusable Percentage Function Demo ---
ECHO.
ECHO 1. What is 35%% of 500?
CALL :percent OF 35 500 result
ECHO Result: %result%
ECHO.
ECHO 2. What percentage is 60 of 240?
CALL :percent ISOF 60 240 result
ECHO Result: %result%%%
ECHO.
ECHO 3. Increase 1500 by 10%%.
CALL :percent INCREASE 1500 10 result
ECHO Result: %result%
ECHO.
ECHO 4. Decrease 80 by 25%%.
CALL :percent DECREASE 80 25 result
ECHO Result: %result%
ECHO.
GOTO :EOF
REM ======================================================================
:percent mode val1 val2 [return_var]
:: Performs various percentage calculations using integer arithmetic.
:: mode [in] - Operation: OF, ISOF, INCREASE, DECREASE
:: val1 [in] - The first value or percentage
:: val2 [in] - The second value or percentage
:: return_var [out] - Variable to store the result
SETLOCAL
set "mode=%~1"
set /a "v1=%~2"
set /a "v2=%~3"
set "res=0"
if /i "%mode%"=="OF" (
set /a "res = (v2 * v1) / 100"
) else if /i "%mode%"=="ISOF" (
set /a "res = (v1 * 100) / v2"
) else if /i "%mode%"=="INCREASE" (
set /a "res = v1 + ((v1 * v2) / 100)"
) else if /i "%mode%"=="DECREASE" (
set /a "res = v1 - ((v1 * v2) / 100)"
) else (
echo ERROR: Invalid mode '%mode%' in :percent function.
)
(
ENDLOCAL
IF "%~4" NEQ "" (SET %~4=%res%) ELSE (ECHO %res%)
)
EXIT /B
Output:
--- Reusable Percentage Function Demo ---
1. What is 35% of 500?
Result: 175
2. What percentage is 60 of 240?
Result: 25%
3. Increase 1500 by 10%.
Result: 1650
4. Decrease 80 by 25%.
Result: 60
Limitations and Considerations
- Integer Overflow: All calculations are still bound by the 32-bit integer limit of
2,147,483,647
. If an intermediate step (likePart * 100
) exceeds this limit, the calculation will fail. - Rounding: This method always rounds down (truncates). For example,
(3 * 100) / 8
results in37
, not37.5
. There is no simple way to perform banker's rounding in pure batch. - Advanced Math: For any calculations requiring higher precision, multiple decimal places, or more complex functions, using a more powerful scripting tool like PowerShell is highly recommended.