Batch percentage help

April 20, 2010 at 14:36:35
Specs: Windows XP, P4 / 4GIG RAM

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
_____________________________________

See More: Batch percentage help

Report •


#1
April 20, 2010 at 17:08:51
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).


Report •

#2
April 20, 2010 at 17:22:00
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.

Report •

#3
April 20, 2010 at 17:54:52
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)
end

and 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 $$.


Report •

Related Solutions

#4
April 20, 2010 at 20:15:51
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)


Report •

#5
April 21, 2010 at 04:29:29
Always first multiply, then divide

out=one*100/two

And if that does not work, make 2 steps

out=one*100
out=out/two


Report •

#6
April 21, 2010 at 05:27:06
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)


Report •

#7
April 21, 2010 at 08:51:43
@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't think it 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.

Report •

Ask Question