Hello,

I need some direction in how to get the percentage of two variables in batch. The problem im having is that decimal values less than 0 return a 0 making it impossible to calculate the percentage, or am i just missing something?

_____________________________

@echo off

set intx=0

set inty=100

set intz=25

set /A intx=(%intz%/%inty%)*100

echo percentage is %intx%%

echo.

pause

_____________________________________

The Command Processor can only handle integers. This line of code: set /A intx=(%intz%/%inty%)*100

gets expanded to:

set /A intx=(25/100)*100

but 25/100 equals 0, because this is integer arithmetic. If you rearrange the operations to (25*100)/100 you'll get the right answer (truncated to 0dp).

Thanks that works. Shame its truncated to 0 though. There really is no way around it without using an external program to do the arithmetic for you and even then you cant work with the values.

the closest you can get is vbscript, which is native to windows. You can write your own calculator using whatever degree of elaboration you have the patience for.

here's a simple one (calc.vbs):

set con=wscript.stdin

set xx=wscript.stdout

set t=wscript.arguments

op=t(1)

one=t(0)

two=t(2)

select case op

case "/"

out=one/two

case "+"

out=one+two

case "-"

out=one-two

case "*"

out=one*two

end select

xx.writeline(out)

endand call it like this (to capture the answer to a var):

for /f %%a in ('cscript /b calc.vbs 12.6 / 18.2') do set xx=%%a

echo %xx%

::----- end

I was lazy and used separate arguments, so the spaces required between the numbers and operators.

of course you can add as many other operators as you want:

modulo, exponentiation, square roots, etc etc.

I wish cscript could be compiled into .exe, but alas...

i saw some for download but they were either "trials" or $$.

That vb script it awesome thanks. I modified it a little for a test but will definatly be using that in the future.

He're what i have.(calcperctest.bat)

@echo off

set intx=0

set inty=33

set intz=100

for /F %%A in ('cscript /b calc.vbs %inty% / %intz%') do set intx=%%A

echo percentage is %intx%%

echo.

pause(calc.vbs)

set con=wscript.stdin

set xx=wscript.stdout

set t=wscript.arguments

op=t(1)

one=t(0)

two=t(2)

out=one/two*100

xx.writeline(out)

Always first multiply, then divide out=one*100/two

And if that does not work, make 2 steps

out=one*100

out=out/two

I should point out VBScript does have an Eval() function, so the following script is valid: On Error Resume Next Dim str For Each a In WScript.Arguments str = str & " " & a Next 'a WScript.Echo Eval(str)

@Razor: yes, that's exactly what i needed! I thought there was something like that but i couldn't remember what it was called (i was using the help .chm and looking for stuff like "macro", so i didn't find it.) Thanks!

@Tvc: i don'tthinkit makes a difference algebraically:

(3*4)/2 = 3*(4/2)

only when mixing different "orders" of families of operations, like addition/subtraction mixed with multiplication/division, then it's important.

Ask Your Question

Weekly Poll

Do you think manufacturers should do more to reduce phone and tablet usage among kids?

Discuss in The Lounge

Poll History