Skip to main content

Batch Script Arithmetic Operators

Arithmetic Operators​

The batch script language supports arithmetic operators to perform sums, differences, multiplications, divisions and modulus.

The following table lists the available arithmetic operators.

OperatorDescriptionExample
+Addition of two operands1 + 2 will give 3
−Subtracts second operand from the first2 − 1 will give 1
*Multiplication of both operands2 * 2 will give 4
/Division of the numerator by the denominator3 / 2 will give 1.5
%Modulus operator and remainder of after an integer/float division3 % 2 will give 1

Example​

The following code snippet shows how the various operators can be used.

Example of Arithmetic Operators
@echo off
set /A a=5
set /A b=10
set /A c= %a%+%b%
echo %c%
set /A c= %a%-%b%
echo %c%
set /A c= %b%*%a%
echo %c%
set /A c=%b%/%a%
echo %c%
set /A c=%b% %% %a%
echo %c%
15
-5
50
2
0

Precedence of Arithmetic operators​

The precedence of arithmetic operators is in the following order:

/ * % + -
note

The expression enclosed inside ( ) gets the highest priority in the precedence.

Let's clarify this with the following example:

PrecedenceExample.bat
@echo off
set /A s = (20 - 5) * 2 + 8 / 2
echo s = %s%
s = 34

Explanation:

  • (20 - 5) gets the highest priority and calculated first giving 15.
  • Then division operator / gets next priority thus 8 / 2 is evaluated giving 4.
  • After this multiplication operator * gets next priority thus 15 * 2 yields 30.
  • Finally, addition operator + performs addition operation 30 + 4 giving final result 34.