Doing MOD calculations in batch
Moderator: DosItHelp
-
- Posts: 1
- Joined: 14 Aug 2007 08:56
Doing MOD calculations in batch
I would love to do a modulo in batch ... does anyone know how to write it??
I figure it's something simlar to ADDITION:
set /a var1=4 + 2
So I was thinking of the logical choices:
set /a var2=4 % 2
- or -
set /a var2=4 MOD 2
Neither of these work. What's wrong? Am I at least close?
I figure it's something simlar to ADDITION:
set /a var1=4 + 2
So I was thinking of the logical choices:
set /a var2=4 % 2
- or -
set /a var2=4 MOD 2
Neither of these work. What's wrong? Am I at least close?
Re: Doing MOD calculations in batch??
Set /a a = 13 %% 5
giving error missing operand
OS Name: Microsoft Windows Server 2008 R2 Standard
OS Version: 6.1.7601 Service Pack 1 Build 7601
OS Name: Microsoft Windows Server 2008 R2 Standard
OS Version: 6.1.7601 Service Pack 1 Build 7601
giving error missing operand
OS Name: Microsoft Windows Server 2008 R2 Standard
OS Version: 6.1.7601 Service Pack 1 Build 7601
OS Name: Microsoft Windows Server 2008 R2 Standard
OS Version: 6.1.7601 Service Pack 1 Build 7601
Re: Doing MOD calculations in batch??
I guess you misunderstood the previous comments in this thread.
If you are running from a command prompt you use one percent symbol.
Code: Select all
Set /a a = 13 % 5
Code: Select all
Set /a a = 13 %% 5
Re: Doing MOD calculations in batch??
It is working here. This is my test:
Antonio
Code: Select all
Microsoft Windows [Versión 6.3.9600]
(c) 2013 Microsoft Corporation. Todos los derechos reservados.
C:\Users\Antonio> cd test
C:\Users\Antonio\Test> type test.bat
@echo off
set /a a = 13 %% 5
echo Result: %a%
C:\Users\Antonio\Test> test
Result: 3
Re: Doing MOD calculations in batch
Kind of late to reply to this but here is a script I wrote that resolved the % issue by using double quotes:
@echo off
title [ Mod Calculator ]
set /p "divnd=Enter a dividend: "
set /a "divnd2=%divnd%"
if %divnd% lss 0 set /a "divnd-=(divnd*2)"
echo:
set /p "divsr=Enter a divisor: "
set /a "qtnt=%divnd%/divsr%"
set /a "mod=%divnd%%%divsr%"
echo:
echo %divnd2% divided by %divsr% = %qtnt%, mod = %mod%
echo:
pause
title
exit /b
@echo off
title [ Mod Calculator ]
set /p "divnd=Enter a dividend: "
set /a "divnd2=%divnd%"
if %divnd% lss 0 set /a "divnd-=(divnd*2)"
echo:
set /p "divsr=Enter a divisor: "
set /a "qtnt=%divnd%/divsr%"
set /a "mod=%divnd%%%divsr%"
echo:
echo %divnd2% divided by %divsr% = %qtnt%, mod = %mod%
echo:
pause
title
exit /b
Re: Doing MOD calculations in batch
Hi juztsteve,
your quote solution is interessting but wrong.
It computes the modulo, but the quotes doesn't escape the percent sign,
instead the complete parsing is broken in your line and works by coincidence.
You can see the full problem by replacing the %divsr% by a fixed number or by inserting spaces
Both cases fails.
Batch is always a source of fun, with unexpected results
jeb
your quote solution is interessting but wrong.
It computes the modulo, but the quotes doesn't escape the percent sign,
instead the complete parsing is broken in your line and works by coincidence.
Code: Select all
set /a "mod=%divnd%%%divsr%"
Is parsed into:
1. set "/a=<the dividend number> %%divsr%" # The expansion of the first variable
2. set "/a=<the dividend number> % divsr%" # Two percent signs are used to create one literal percent sign
3. set "/a=<the dividend number> % divsr" # The trailing percent sign is removed, because there are no more percent signs in the line
4. set "/a=<the dividend number> % <the divisor>" # set/a is able to replace variable names by itself, without the need of percent/delayed expansion
Code: Select all
set /a "mod=%divnd%%16"
set /a "mod=%divnd% % %divsr%"
Batch is always a source of fun, with unexpected results
jeb
Re: Doing MOD calculations in batch
You should be using this instead:
Code: Select all
set /a divnd2 = divnd
Code: Select all
set /a qtnt = divnd / divsr
Code: Select all
set /a mod = divnd %% divsr
Code: Select all
set /a "divnd2=divnd"
Code: Select all
set /a "qtnt=divnd/divsr"
Code: Select all
set /a "mod=divnd%%divsr"