批处理文件中的浮点除法 [英] Floating point division in a batch file

查看:18
本文介绍了批处理文件中的浮点除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 dos 批处理中进行浮点除法.

I 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

返回一个整数.

可以吗?

推荐答案

这样的批处理文件不支持浮点运算.但是,这篇文章 建议使用外部脚本文件进行计算的解决方法.脚本文件应该使用某种 eval 函数来计算作为参数传递的表达式并返回结果.这是执行此操作的示例 VBScript 文件 (eval.vbs):

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%

当然,你会得到一个字符串形式的结果,但总比没有好,你可以将得到的结果作为另一个表达式的一部分传递给 eval 脚本.

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.

这篇关于批处理文件中的浮点除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆