Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math on batch (win)

I am developing a CMD batch. I want to do some math in it. This formula: (x+1)100:y

So in batch, x = %x%, and y = %y%. I know how to set the variables. Now, how can batch calculate this? (WINDOWS CMD)

Do I need something extra?

(I need this to be available to users of Windows XP to 7.)

like image 906
Deniz Zoeteman Avatar asked Dec 08 '09 19:12

Deniz Zoeteman


2 Answers

The set command supports some limited calculation. In your case, you want: set /a result=(100*x)/y

Run set /? to see full documentation on what is supported. The /a switch does automatic variable substitution, so you can use x instead of %x%.

like image 123
Kevin Avatar answered Oct 13 '22 05:10

Kevin


@echo off

set x=42
set y=5

set /a z = 100 * x / y

echo %z%

The set /a command allows you to assign the result of a mathematical expression to a variable (at that, the %'s around variable names in these expressions aren't required). See set /? for the list of supported operators and details.

Note that Windows batch files don't support floating-point arithmetic, so if an expression result is a fractional number, only the integer part will be counted. For example, 3/2 evaluates as 1 and 2/3 as 0.

like image 24
Helen Avatar answered Oct 13 '22 04:10

Helen