views:

372

answers:

2

Hi need to do a floating-point division in a dos batch.

I didn't find a way to do it. Something like this :

SET /A Res=10/3

returns a integer number.

Is it possible to do it ?

+1  A: 

Batch files as such do not support the floating point arithmetic. However, this article suggests a workaround that uses an external script file to do calculations. The script file should use some sort of eval function to evaluate the expression passed as an argument and return the result. Here's a sample VBScript file (eval.vbs) that does this:

WScript.Echo Eval(WScript.Arguments(0))

You can call this external script from your batch file, specify the expression to be evaluated and get the result back. For example:

@echo off
for /f %%n in ('cscript //nologo eval.vbs "10/3"') do (
  set res=%%n
)
echo %res%

Of course, you'll get the result as a string, but it's better than nothing anyway, and you can pass the obtained result to the eval script as part of another expression.

Helen
You can't pass the result to another eval script – not in the general case at least. The numbers are formatted for your current locale. In my case this means that the comma `,` is used as decimal point. This leads to a syntax error if I were to put the result into the script again.
Joey
+1  A: 

According to this reference, there is no floating point type in DOS batch language:

Although variables do exist in the DOS batch programming language, they are extremely limited. There are no integer, pointer or floating point variable types, only strings.

I think what you are trying to do will be impossible without implementing your own division scheme to calculate the remainder explicitly.

ire_and_curses
Even though they are only strings you can do signed 32-bit integer arithmetic with `set /a`.
Joey