TExcelTAD
Home › Windows CMD & Batch › set /a Arithmetic

set /a Arithmetic

Plain set stores text: set x=2+3 gives you the three characters 2+3. To make CMD calculate, use set /a. It evaluates an expression with 32-bit signed integers and stores the result as a string of digits.

cmd · try it yourself
@echo off
set x=2+3
echo Text:   %x%
set /a y=2+3
echo Number: %y%
Text:   2+3
Number: 5

Run interactively (at the prompt, not in a batch file), set /a also prints the result, which makes the Command Prompt a quick calculator:

cmd
C:\Users\ada>set /a 17*3
51

Operators

set /a supports the C-style operators, with the usual precedence (multiplication before addition, and parentheses to override):

Operators Meaning
+ - * / add, subtract, multiply, integer divide
% (written %% in a batch file) remainder (modulo)
( ) grouping
<< >> bit shift
& | ^ ~ bitwise and, or, xor, not
! logical not (!0 is 1, anything else gives 0)
= += -= *= /= %= ... assignment and compound assignment
, separates several expressions
cmd · try it yourself
@echo off
set /a a=7+5*2
set /a b=(7+5)*2
set /a c=17/5
set /a d=17%%5
set /a e=-17/5
echo a=%a% b=%b% c=%c% d=%d% e=%e%
a=17 b=24 c=3 d=2 e=-3

Division is integer division: the fractional part is thrown away (truncated toward zero, so -17/5 is -3, not -4). There are no decimals in set /a. If you need them, scale your numbers, for example by working in cents instead of euros. Dividing by zero is an error (Divide by zero error.) and leaves the variable unchanged.

Why %% for modulo?

In a batch file % introduces a variable (%x%) or an argument (%1), so a literal percent sign must be doubled: %%. At the interactive prompt one % is enough. Forgetting this is the most common set /a bug in scripts.

Variables inside expressions

Inside set /a you can write variable names without percent signs. CMD looks up each name itself, and an undefined or non-numeric variable counts as 0:

cmd · try it yourself
@echo off
set /a price=250
set /a qty=4
set /a total=price*qty
echo total=%total%
set /a missing_plus_one=nothing_here+1
echo missing_plus_one=%missing_plus_one%
total=1000
missing_plus_one=1

Writing the bare name is not just shorter; it also behaves better inside loops and blocks, where %total% might have been expanded too early (see setlocal & Delayed Expansion).

Compound assignment: counters

+=, -=, *= and friends update a variable in place. Quote the expression when it contains characters CMD treats specially (<<, >>, &, |, ^), otherwise CMD reads them as redirection or a pipe:

cmd · try it yourself
@echo off
set /a count=0
set /a count+=1
set /a count+=1
set /a count*=10
echo count=%count%
set /a "flags=5 & 3"
set /a "shifted=1 << 4"
echo flags=%flags% shifted=%shifted%
count=20
flags=1 shifted=16

Without the quotes, set /a flags=5 & 3 would run set /a flags=5 and then try to run a command called 3.

Several assignments on one line

A comma separates independent expressions, so you can initialise several variables at once:

cmd · try it yourself
@echo off
set /a width=12, height=5, area=width*height, perimeter=2*(width+height)
echo %width% x %height%: area %area%, perimeter %perimeter%
12 x 5: area 60, perimeter 34

The expressions run left to right, so area can already use the new width and height.

Number bases

A leading 0x means hexadecimal and a leading 0 means octal. The result is always printed in decimal:

cmd · try it yourself
@echo off
set /a h=0xFF
set /a o=010
set /a d=10
echo hex 0xFF = %h%, octal 010 = %o%, decimal 10 = %d%
hex 0xFF = 255, octal 010 = 8, decimal 10 = 10

The octal rule bites when numbers come from text with leading zeros, such as a month 08 or a time 09: 08 and 09 are invalid octal numbers and set /a reports an error. Strip leading zeros (or prefix the number with 1 and subtract 100) before calculating:

cmd · try it yourself
@echo off
set "month=08"
set /a next=1%month%-100+1
echo next month: %next%
next month: 9

1%month% becomes the text 108, which is a normal decimal number.

Limits

Numbers are 32-bit signed integers: the range is -2147483648 to 2147483647. Results that leave that range wrap around without any warning:

cmd · try it yourself
@echo off
set /a big=2147483647
set /a bigger=big+1
echo %big% + 1 = %bigger%
2147483647 + 1 = -2147483648

For large numbers, decimals, or real math functions, call PowerShell from your batch file, for example powershell -NoProfile -Command "[math]::Round(10/3, 2)", and capture the result with for /f.

A small calculation script

cmd · try it yourself
@echo off
set /a seconds=7384
set /a h=seconds/3600, m=seconds%%3600/60, s=seconds%%60
echo %seconds% seconds = %h% h %m% min %s% s
7384 seconds = 2 h 3 min 4 s

Comparison with other shells

Task CMD Bash PowerShell
calculate set /a x=a*b x=$((a*b)) $x = $a * $b
modulo %% in a script % %
decimals no no (use bc) yes
increment set /a n+=1 ((n++)) $n++

Learn more: Bash Variables, PowerShell Operators, Batch Variables.

Test yourself

What does set /a r=7/2 store in r?

  1. 3.5
  2. 3
  3. 4
  4. 7/2

Inside a batch file, how do you write 'n modulo 3' with set /a?

  1. set /a r=n%3
  2. set /a r=n%%3
  3. set /a r=n mod 3
  4. set /a r=%n%/3

Why does set /a x=09+1 fail?

  1. Numbers can't have two digits
  2. A leading 0 means octal, and 9 is not an octal digit
  3. The + must be quoted
  4. x is a reserved name