Guy
Guy

Reputation: 6522

How does division work?

I've just started learning batach and I have some problems understanding division. In other languages, when I would do this:

25 /= 10

The result would be 2.

But in batch, when I do 25 /= 10, the result is still 25. Why is that? How can I get the "2" out of 25 when I divide by 10?

Upvotes: 0

Views: 121

Answers (2)

Ken White
Ken White

Reputation: 125728

The sample you show makes no sense. `25/=10' tries to divide and assign to the left side, which is an integer.

I think what you're actually trying to do is more like this:

set test=25
set /a test/=10

If that's the case, this works perfectly fine:

@echo off
set test=25
@echo Test is %test%
set /a test/=10
@echo Now Test is %test%
pause

The output is:

D:\TempFiles>divtest
Test is 25
Now Test is 2
Press any key to continue . . .

Upvotes: 1

Martin Dinov
Martin Dinov

Reputation: 8825

You need to surround the variable names with %% to get their value.

So here's how:

@echo off
set num1=25
set num2=10
set /a result=%num1%/%num2%
echo num1=%num1%
echo num2=%num2%
echo %result%

Output:

> foo.bat
num1=25
num2=10
2

Upvotes: 0

Related Questions