Calculation (Arithmetic)
Sometimes you want a batch file to count iterations, add up a total, or generate a random number like a die roll.
To do math in Command Prompt, add the /a option to the set command. This article covers the basics as well as the pitfalls that are easy to trip over.
Basic Usage
set /a variable_name=expression
set /a RESULT=10 + 5
echo %RESULT%
The result is assigned to the variable RESULT. When you type the command directly into Command Prompt, the result is displayed as is. Inside a batch file it is not displayed, so use echo if you need to print it.
Inside a set /a expression you can write variable names as they are. As in set /a TOTAL=PRICE * 3, the variable’s value is used without enclosing it in % or !. A variable that does not exist is treated as 0.
Available Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | set /a X=7 + 3 | 10 |
- | Subtraction | set /a X=7 - 3 | 4 |
* | Multiplication | set /a X=7 * 3 | 21 |
/ | Division (integer) | set /a X=7 / 3 | 2 |
% | Remainder | set /a X=7 %% 3 (in a batch file) | 1 |
( ) | Specify precedence | set /a X=(2 + 3) * 4 | 20 |
Multiplication, division, and remainder are evaluated before addition and subtraction.
The % that gives the remainder must be doubled to %% inside a batch file. When you type it directly into Command Prompt, write only a single %.
Updating a Variable (Counters and Totals)
You can use +=, -=, *=, /=, and %%=, which assign and calculate in one step. (%%= is the form used in a batch file; type %= when entering it directly at the prompt.)
set COUNT=0
set /a COUNT+=1
set /a COUNT+=1
echo %COUNT%

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Here is an example that adds up a total inside a for loop. set /a can read variables directly inside the loop, but delayed expansion (!variable_name!) is needed to display the running total with echo inside the loop.
@echo off
setlocal EnableDelayedExpansion
set SUM=0
for %%N in (10 20 30) do (
set /a SUM+=%%N
echo Running total: !SUM!
)
echo Total: !SUM!
endlocal
See the article on delayed expansion for details.
Decimals Are Not Supported
set /a handles integers only. The fractional part of a division result is truncated.
set /a X=7 / 2
echo %X%
If you need decimals, one approach is to multiply by 100 or similar beforehand and calculate with integers.
set PRICE=1980
set /a TAX=PRICE * 10 / 100
set /a TOTAL=PRICE + TAX
echo Tax: %TAX% / Total with tax: %TOTAL%
If you need exact decimal calculations, you can also call PowerShell.
powershell -NoProfile -Command "[math]::Round(1980 * 1.1)"
Generating Random Numbers
%RANDOM% returns a random number from 0 to 32767. Using the remainder lets you narrow the range.
set /a DICE=%RANDOM% %% 6 + 1
echo %DICE%
%RANDOM% %% 6 gives 0 to 5, so + 1 shifts it to 1 to 6.
Hexadecimal and Bitwise Operations
Prefix a number with 0x to specify hexadecimal.
set /a X=0xFF
echo %X%
Bitwise operators include & (AND), | (OR), ^ (XOR), <<, and >>. These are also special characters in Command Prompt, so you must enclose the whole expression in double quotation marks.
set /a "X=6 & 3"
set /a "Y=1 << 4"
echo %X% %Y%
If you write set /a X=6 & 3 without quotation marks, & is interpreted as a command separator and the result is not what you intended.
Pitfalls
The Range Is 32-Bit Integers
The numbers you can handle range from -2147483648 to 2147483647. If a calculation result goes beyond that range, no error occurs; the value overflows and becomes a wrong value. However, writing an out-of-range number such as 2147483648 directly causes an error.
set /a X=2147483647 + 1
echo %X%
Be careful when working with large numbers, such as when calculating file sizes.
Numbers Starting with 0 Are Treated as Octal
Numbers that start with 0, such as 08 or 09, are interpreted as octal. Octal has no 8 or 9, so this causes an error.
set /a X=08 + 1
This problem occurs when you use values extracted from %TIME%, such as minutes or seconds such as “08” or “09”, directly in a calculation. Putting 1 in front and subtracting 100 makes it be treated as decimal.
set MIN=%TIME:~3,2%
set /a MIN=1%MIN% - 100
echo %MIN%
If MIN is 08, then 108 - 100 gives 8.
Dividing by 0 Causes an Error
set /a X=10 / 0
If the divisor is a variable, use if to confirm it is not 0 before calculating.
Displaying a Result with Zero Padding
For sequential file names (001, 002), it is handy to add 1000 and then take the right-hand digits.
set /a NUM=1000 + 7
echo %NUM:~1%
How to extract part of a string is explained in detail in the article on string operations.
Summary
| What you want to do | How to write it |
|---|---|
| Store a result in a variable | set /a X=1 + 2 |
| Count up | set /a COUNT+=1 |
| Get a remainder (in a batch file) | set /a X=10 %% 3 |
| Random number from 1 to N | set /a X=%RANDOM% %% N + 1 |
| Bitwise operation | set /a "X=6 & 3" (enclose in quotation marks) |
| Decimal calculation | Multiply by 100 and use integers, or use PowerShell |