Page 1 of 1

Arithmetic Operation

Posted: 12 Apr 2020 21:39
by claudineskiles
Type1:
@echo off
set CURRMonth=09
set /a CURRMonth+=1
echo %CURRMonth% > Output.txt
exit

Output is: 1

Type2:
@echo off
set CURRMonth=9
set /a CURRMonth+=1
echo %CURRMonth% > Output.txt
exit

Output is: 10


What I am trying to solve is, no matter how many number of '0' assigned infront of the value the result should be the same
so even if I give CURRMonth=9 or CURRMonth=09 or CURRMonth=009 it should give the same result 10. Is it possible?

Re: Arithmetic Operation

Posted: 13 Apr 2020 04:46
by aGerman
Numbers with a preceding zero are treated as octal numbers where 8 and 9 are illegal digits. Prepend string 100 and calculate the modulo of the resulting string (which is treated as a decimal number) using 100 as divisor.

Code: Select all

set "mm=09"
set /a "m=100%mm% %% 100"
echo %m%
set /a "m+=1"
echo %m%
Steffen