Skip to main content

How to Calculate a Percentage in Batch Script

Calculating a percentage is a common requirement for many scripts, especially those that generate reports on disk space, task completion, or data processing. While the mathematical formula is simple ((Part / Total) * 100), implementing this in a batch script has a critical catch: the batch command processor can only handle integer arithmetic.

This guide will teach you the correct, non-obvious way to calculate a percentage in a batch script by rearranging the formula to work with integers. You will also learn how to handle the limitations of this method, such as division by zero, and see the modern PowerShell alternative for when you need decimal precision.

The Challenge: Integer-Only Arithmetic

The SET /A command is the built-in tool for performing math in batch scripts. However, it does not support floating-point numbers or decimals. When it performs division, it truncates any fractional part. This completely breaks the standard percentage formula.

Example of script with error: let's say we have 50 used items out of a total of 200.

@ECHO OFF
SET "Part=50"
SET "Total=200"

REM This will FAIL.
SET /A "Percentage = (%Part% / %Total%) * 100"
ECHO The percentage is: %Percentage%%%

Output:

The percentage is: 0%
note

This is wrong. The problem is that (50 / 200) is 0.25. SET /A truncates this to 0 before it multiplies by 100, resulting in 0 * 100 = 0.

The Core Method: Rearranging the Formula

To get around the integer truncation problem, we must change the order of operations. By multiplying before we divide, we can keep the numbers large enough to work with.

The Correct Formula: (Part * 100) / Total

Using this formula with our previous example: (50 * 100) / 200 becomes 5000 / 200, which SET /A can correctly calculate as 25.

Basic Example: Calculating a Simple Percentage

This script uses the correct, rearranged formula to calculate the percentage of completed tasks.

@ECHO OFF
SET "TasksCompleted=60"
SET "TotalTasks=240"

ECHO Calculating the percentage of completed tasks...

SET /A "Percentage = (%TasksCompleted% * 100) / %TotalTasks%"

ECHO.
ECHO Result: %Percentage%%% of tasks are complete.

Output:

Calculating the percentage of completed tasks...

Result: 25% of tasks are complete.

Handling Decimals (The PowerShell Method)

The pure-batch method can only ever give you a whole number as a result. If you need decimal precision (e.g., 25.5%), you must use a more powerful tool. The best built-in option is PowerShell.

This script calls PowerShell to perform a floating-point calculation and format the result.

@ECHO OFF
SET "Part=51"
SET "Total=200"
SET "DecimalPercent="

FOR /F %%P IN (
'powershell -Command "($env:Part / $env:Total).ToString('P1')"'
) DO (
SET "DecimalPercent=%%P"
)

ECHO The precise percentage is: %DecimalPercent%

Output:

The precise percentage is: 25.5 %
note

ToString('P1'): This PowerShell method formats the number as a Percentage with 1 decimal place.

Common Pitfalls and How to Solve Them

The Critical Importance of Operation Order

This is the main pitfall. Forgetting to multiply first will always lead to an incorrect result of 0 if the Part is smaller than the Total.

Solution: Always use the formula (Part * 100) / Total.

Division by Zero

If your Total variable is 0, the SET /A command will crash the script with a "Divide by zero error."

Solution: Always add a check to ensure your denominator is not zero before you perform the calculation.

IF %TotalTasks% EQU 0 (
ECHO Total tasks is zero, cannot calculate percentage.
) ELSE (
SET /A "Percentage = (%TasksCompleted% * 100) / %TotalTasks%"
ECHO Result: %Percentage%%% complete.
)

Large Numbers and the 32-bit Limit

SET /A uses 32-bit signed integers. The maximum value it can hold is 2,147,483,647. If Part * 100 exceeds this value, your calculation will overflow and produce an incorrect (often negative) result.

Solution: For very large numbers, PowerShell is again the more robust solution, as it handles 64-bit integers ([long]) automatically.

Practical Example: Calculating Disk Space Usage

This is a classic use case. The script gets the total size and free space of the C: drive and calculates the percentage of space used.

@ECHO OFF
SETLOCAL
ECHO --- C: Drive Usage Report ---
ECHO.
ECHO Getting disk space information...

REM Use WMIC to get the size and free space in bytes.
FOR /F "tokens=1,2" %%A IN ('WMIC LOGICALDISK WHERE "DeviceID='C:'" GET FreeSpace^,Size /VALUE') DO (
IF "%%A"=="FreeSpace" SET "FreeSpace=%%B"
IF "%%A"=="Size" SET "TotalSpace=%%B"
)

IF %TotalSpace% EQU 0 (
ECHO [ERROR] Could not determine total disk space.
GOTO :End
)

SET /A "UsedSpace = %TotalSpace% - %FreeSpace%"

REM The numbers are huge, so we divide by 1MB for the calculation to avoid overflow.
SET /A "UsedMB = %UsedSpace% / 1048576"
SET /A "TotalMB = %TotalSpace% / 1048576"

SET /A "PercentUsed = (%UsedMB% * 100) / %TotalMB%"

ECHO Percentage of C: drive used: %PercentUsed%%%

:End
ENDLOCAL

Conclusion

Calculating a percentage in a batch script is simple once you know the integer arithmetic trick.

  • The standard formula (Part / Total) * 100 will fail.
  • You must rearrange the formula to multiply first: (Part * 100) / Total.
  • Always check for a zero denominator before dividing to prevent your script from crashing.
  • For calculations requiring decimal precision or numbers larger than 2.1 billion, you must use a more powerful tool like PowerShell.