views:

567

answers:

4

Hello,

i am developing a CMD batch kind of thing.

Now, i want to do some math in it. This formula:

(x+1)100:y

or for guys who are not so good in math:

x+1, answer times 100, answer devided by y.

So in batch, x = %x%, and y = %y%.

So, 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.)

Thanks.

+5  A: 

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 also does automatic variable subsitution, so you can use x instead of %x%.

Iceman
Thank you! It works!
YourComputerHelpZ
and what if i want to say like x+1?
YourComputerHelpZ
never mind, got it.
YourComputerHelpZ
+1  A: 
@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.

Helen
A: 

DON'T do maths in batch. If you are doing percentage, you might have decimals and batch doesn't support decimals.(floating). Use vbscript or something better.

save the below as caculate.vbs

Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
x = objArgs(0)
y = objArgs(1)
WScript.Echo ( x + 1 ) * 100  / y

and on command line

c:\test> cscript //nologo calculate.vbs 4 5
ghostdog74
i don't need decimals for what i'm working on.
YourComputerHelpZ
and btw; i forgot that it wasn't x+1 but y+1. So like (x * 100) / ( y + 1 )
YourComputerHelpZ
A: 

any way to do this with pocket cmd v5.00 or v6.00

ce prog