Arithmetic Operation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
claudineskiles
Posts: 1
Joined: 12 Apr 2020 21:37

Arithmetic Operation

#1 Post by claudineskiles » 12 Apr 2020 21:39

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?

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Arithmetic Operation

#2 Post by aGerman » 13 Apr 2020 04:46

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

Post Reply