Arithmetic Operation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
navanees
Posts: 11
Joined: 20 Jul 2016 16:31

Arithmetic Operation

#1 Post by navanees » 24 Mar 2017 18:08

Please Help:

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?

-NN

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Arithmetic Operation

#2 Post by ShadowThief » 24 Mar 2017 19:16

Depends on whether or not you know how many zeroes are in front of the 9.

If you do, you can stick a 1 at the front of the number and subtract whatever power of 10 there is until only the 9 is left.

For example

Code: Select all

@echo off
set CURRMonth=09
set /a CURRMonth=1%CURRMonth%-100
set /a CURRMonth+=1
echo %CURRMonth% > Output.txt
exit

or

Code: Select all

@echo off
set CURRMonth=009
set /a CURRMonth=1%CURRMonth%-1000
set /a CURRMonth+=1
echo %CURRMonth% > Output.txt
exit
or

Code: Select all

@echo off
set CURRMonth=0009
set /a CURRMonth=1%CURRMonth%-10000
set /a CURRMonth+=1
echo %CURRMonth% > Output.txt
exit


You get the idea

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Arithmetic Operation

#3 Post by Sounak@9434 » 24 Mar 2017 23:48

Or you might try this method to remove zeroes from the beginning.

Code: Select all

@echo off
call :rezero 000000000000000000000000000000250045644500000690000 _out
echo %_out%
pause>nul
:rezero [Digit] [Variable to store output]
if not defined _rzer setlocal&set "_rzer=%~1"&set "_over=%~2"
if "%_rzer:~0,1%"=="0" set "_rzer=%_rzer:~1%"&goto :rezero
endlocal&set "%_over%=%_rzer%"


Sounak

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

Re: Arithmetic Operation

#4 Post by aGerman » 25 Mar 2017 04:53

so even if I give CURRMonth=9 or CURRMonth=09 or CURRMonth=009 it should give the same result 10. Is it possible?

A preceding zero marks a number as octal number. Digits 8 and 9 don't exist in the octal numbering system thus, 08 and 09 are invalid.
To remove any leading 0 you can use a FOR /F loop

Code: Select all

for /f "tokens=* delims=0 %%i in ("%CURRMonth%") do if "%%i"=="" (set "CURRMonth=0") else set "CURRMonth=%%i"


But I think there is a question behind the scenes. Your variable name implies that you want to know how to add 1 to the current month.
1) Single-digit months are often preceded with a zero. You may want to preserve it.
2) 01 should follow after 12

Code: Select all

:: result is 101...112
set /a "CURRMonth=1%CURRMonth% %% 112 + 101"
:: last 2 digits
echo %CURRMonth:~-2%


Steffen

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Arithmetic Operation

#5 Post by ShadowThief » 25 Mar 2017 07:45

aGerman wrote:A preceding zero marks a number as octal number. Digits 8 and 9 don't exist in the octal numbering system thus, 08 and 09 are invalid.

Only if they're using the /a flag. A regular set statement will treat it like a string.

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

Re: Arithmetic Operation

#6 Post by aGerman » 25 Mar 2017 08:22

ShadowThief wrote:Only if they're using the /a flag.

... or in an IF statement (using the textual comparison operators), or in a FOR /L loop, or for some options in a FOR /F loop ... Of course you're right. Everything is a string in the first place. Only if an internal conversion was triggered it becomes a number. I left it out in order to avoid too much confusion :wink:

Steffen

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Arithmetic Operation

#7 Post by Jer » 25 Mar 2017 23:27

This code narrows down how many leading zeroes proceed the month number.

If your data is 000000002, removing all but the last 2 digits with %var:~-2% guarantees that
you have months 1-12, and adding leading zeroes beforehand helps in case the month number is
1-9 with no leading zeroes. "1" is joined to the beginning of the new string, which becomes 1002.
Then subtract 1,000 from this value followed by a calculated mod 12 (%%12), then add 1
and the result is the month number of the next month: 3

Code: Select all

@echo off
echo( & echo Calculates next month number when current month could have
echo any number of leading zeroes or none.  Example entry:  15 12
echo Just press Enter key to exit. & echo(
:top
Set "numbZ=" & Set "CURRMonth=" & Set "stringZ=000"
echo( & echo Enter a count of leading zeroes and a space followed by month number 1-12
Set /p "numbZ="

If "%numbZ%" equ "" exit /b

For /F "tokens=1* delims= " %%a In ("%numbZ%") Do Set "numbZ=%%a" & Set "monthNo=%%b"
If "%monthNo%" equ "" exit /b

For /L %%n In (1,1,%numbZ%) Do Call Set "stringZ=0%%stringZ%%"

Set "CURRMonth=1%stringZ:~-2%%monthNo%"
Set /A "NEXTMonth=((%CURRMonth%-1000)%%12)+1"

echo current month string: %stringZ:~3%%monthNo%   next month: %NEXTMonth%
goto :top


comments edited
Last edited by Jer on 26 Mar 2017 09:53, edited 3 times in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Arithmetic Operation

#8 Post by ShadowThief » 26 Mar 2017 01:04

aGerman wrote:
ShadowThief wrote:Only if they're using the /a flag.

... or in an IF statement (using the textual comparison operators), or in a FOR /L loop, or for some options in a FOR /F loop ... Of course you're right. Everything is a string in the first place. Only if an internal conversion was triggered it becomes a number. I left it out in order to avoid too much confusion :wink:

Steffen

I only mentioned it in case he was wondering why his code was working in the first place.

Post Reply