Calculation (Arithmetic)

Maintained on

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

Syntax
set /a variable_name=expression
set /a RESULT=10 + 5
echo %RESULT%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>set /a RESULT=10 + 5
15
C:\users\user>

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.

Do not wrap variables in % inside the expression

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

OperatorMeaningExampleResult
+Additionset /a X=7 + 310
-Subtractionset /a X=7 - 34
*Multiplicationset /a X=7 * 321
/Division (integer)set /a X=7 / 32
%Remainderset /a X=7 %% 3 (in a batch file)1
( )Specify precedenceset /a X=(2 + 3) * 420

Multiplication, division, and remainder are evaluated before addition and subtraction.

Write the remainder operator as %% in batch files

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.)

Increment
set COUNT=0
set /a COUNT+=1
set /a COUNT+=1
echo %COUNT%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %COUNT%
2
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

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

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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかる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.

sum.cmd
@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
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sum.cmd
Running total: 10
Running total: 30
Running total: 60
Total: 60
C:\users\user>

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%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %X%
3
C:\users\user>

If you need decimals, one approach is to multiply by 100 or similar beforehand and calculate with integers.

Calculate
set PRICE=1980
set /a TAX=PRICE * 10 / 100
set /a TOTAL=PRICE + TAX
echo Tax: %TAX% / Total with tax: %TOTAL%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>Display the tax and the total
Tax: 198 / Total with tax: 2178
C:\users\user>

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.

A
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%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %X%
255
C:\users\user>

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%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %X% %Y%
2 16
C:\users\user>

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%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %X%
-2147483648
C:\users\user>

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
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>set /a X=08 + 1
Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).
C:\users\user>

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.

Safely
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
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>set /a X=10 / 0
Divide by zero error.
C:\users\user>

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.

Zero-pad
set /a NUM=1000 + 7
echo %NUM:~1%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %NUM:~1%
007
C:\users\user>
チェック

How to extract part of a string is explained in detail in the article on string operations.

Summary

What you want to doHow to write it
Store a result in a variableset /a X=1 + 2
Count upset /a COUNT+=1
Get a remainder (in a batch file)set /a X=10 %% 3
Random number from 1 to Nset /a X=%RANDOM% %% N + 1
Bitwise operationset /a "X=6 & 3" (enclose in quotation marks)
Decimal calculationMultiply by 100 and use integers, or use PowerShell

Quiz

Practice Problem

Which is the correct way to assign the remainder of 10 divided by 3 to the variable R inside a batch file?

Choose one answer.

  1. set /a R=10 %% 3
  2. set R=10 % 3
  3. set /a R=10 mod 3

Practice Problem

What is the value of X after running set /a X=7 / 2?

Choose one answer.

  1. 3 (the fractional part is truncated)
  2. 3.5
  3. 4 (rounded)
#Command Prompt #Batch File #Calculation #set #Variable