How to do math in batch?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
petertohen
Posts: 1
Joined: 24 Sep 2017 03:36

How to do math in batch?

#1 Post by petertohen » 24 Sep 2017 03:39

Im coding a .bat game and I need a way to increase/decrease health and money. I just want a smallish command instead of:

if %money%=05 set %money% 00
if %money%=10 set %money% 05
if %money%=15 set %money% 10
if %money%=20 set %money% 15
if %money%=25 set %money% 20
if %money%=30 set %money% 25

and etc. Is there just a command that subtracts or adds a specific number to a variable? :?:
http://www.dostips.com/DtTipsArithmetic.php
Last edited by aGerman on 24 Sep 2017 04:42, edited 1 time in total.
Reason: changed the meaningless topic name

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

Re: How to do math in batch?

#2 Post by aGerman » 24 Sep 2017 04:50

The command you're looking for is SET /A.

Code: Select all

set /a "money-=5"
... subtracts 5 from the former value of %money%. Note that 05 is an octal number. For the beginning you should rather avoid leading zeros.

Also:
if %money%=05
The = is the assignment operator. The equality comparison operator would have been the ==.

set %money% 00
Double false. 1) the assignment operator is missing. 2) You have to assign to the variable name (without percent signs).

Steffen

Post Reply