如何使用基本的批处理数学命令得出小数答案?

How do I use basic batch math commands to come up with decimal answers?

我正在制作一个批处理文件,这样我就可以告诉它我需要使用什么样的公式,它会告诉我需要输入哪些变量。现在,我正在对它进行编码,以便它可以找到三角形的面积。

我已经编写了代码,它会询问三角形的底边、高度和单位。如您所知,公式很简单,底数 x 高度 / 2。所以我测试了代码以查看它是否有效。但我注意到,当它用奇数除以 2 时,它不会给出小数答案。相反,它会向下舍入一个数字。 (例如 15/2=7)

这是我的代码:

    set /p TriangleB=How long is the base of the triangle? (Don't put the unit, just put the number):
    set /p TriangleH=What is the height of the triangle? (Don't put the unit, just put the number):
    set /p TriangleAreaUnit=What unit is the triangle being measured in?:
    set /a TriangleArea=%TriangleB% * %TriangleH% / 2
    echo The area of the triangle is %TriangleArea% %TriangleAreaUnit%.
    pause >nul
    goto :EOF

它没有给我任何错误信息,而且一切似乎 运行 都很顺利,我只是不明白为什么它不给我小数答案。

使用这一行基本脚本

    Execute "Wscript.echo " & wscript.Arguments(0)

使用

    cscript //nologo script.vbs "2.5*2.5"

如何使用

C:\Users\User>cscript //nologo "C:\Users\User\Desktop\New Text Document.vbs" "2.5*2.5"
6.25

第二行是如何将刚刚编写的程序作为命令提示符命令使用。 cscript //nologo "c:\somefolder\script.vbs" "%TriangleB% * %TriangleH% / 2"。请注意,如果表达式包含空格,则必须用引号括起来。放入命令提示符变量使用命令。 for /f "delims=" %%A in ('cscript //nologo c:\somefolder\script.vbs "%TriangleB% * %TriangleH% / 2"') Do Set Result=%%A

所以

C:\Users\User>Set TriangleB=5.5

C:\Users\User>Set TriangleH=3.5

C:\Users\User>for /f "delims=" %A in ('cscript //nologo "C:\Users\User\Desktop\New Text Document.vbs" "%TriangleB% * %TriangleH% / 2"') Do Set Result=%A

C:\Users\David Candy>Set Result=9.625

记住在批量使用 %%A 和交互(即打字)使用 %A

在 HTA 中做同样的事情(将网页重命名为 .hta,使其像程序一样运行)

<html>
    <head>
        <script language="VBScript">
            Sub Calculate
                Answr.innertext = tb1.value * tb2.value /2
            End Sub
        </script>
    </head>
    <body>

<p><INPUT Name=tb1 TYPE=Text Value="Height">
<p><INPUT Name=tb2 TYPE=Text Value="Width">
<p><INPUT NAME="Search" TYPE="BUTTON" VALUE="Calc" OnClick=Calculate>
<div ID=Answr></div>
</body>
</html>

并且仅在 vbscript 中

 Wscript.echo InputBox("Height") * Inputbox("width") /2

批处理数学只能处理 32 位整数,但有一些方法可以解决此限制。最简单的方法是使用 PowerShell 并利用 for /f 让您将命令的输出存储在变量中。

@echo off

set /p "TriangleB=How long is the base of the triangle? (Don't put the unit, just put the number): "
set /p "TriangleH=What is the height of the triangle? (Don't put the unit, just put the number): "
set /p "TriangleAreaUnit=What unit is the triangle being measured in?: "

for /f %%A in ('powershell %TriangleB% * %TriangleH% / 2') do set TriangleArea=%%A

echo The area of the triangle is %TriangleArea% %TriangleAreaUnit%.
pause >nul

只要您使用的 Windows 版本早于 XP,就可以使用。

您可以像这样使用批处理文件进行尝试:

@echo off
set /p "TriangleB=How long is the base of the triangle ? (Don't put the unit, just put the number): "
set /p "TriangleH=What is the height of the triangle ? (Don't put the unit, just put the number): "
set /p "TriangleAreaUnit=What unit is the triangle being measured in ?: "
Call :makevbs %TriangleB% %TriangleH%
echo The area of the triangle is %TriangleArea% %TriangleAreaUnit%^2.
pause >nul
Exit /b

:makevbs
echo wscript.echo Eval( wscript.Arguments(0^) * wscript.Arguments(1^) / 2^) > "%tmp%\%~n0.vbs"
for /f %%A in ('cscript /nologo "%tmp%\%~n0.vbs" %~1 %~2') do set TriangleArea=%%A
exit /b

对于手头的任务,您可以通过某种定点算法解决限制,如以下示例代码所示:

set /p TriangleB=How long is the base of the triangle? (Don't put the unit, just put the number):
set /p TriangleH=What is the height of the triangle? (Don't put the unit, just put the number):
set /p TriangleAreaUnit=What unit is the triangle being measured in?:
set /a TriangleArea=%TriangleB% * %TriangleH% * 10 / 2
echo The area of the triangle is %TriangleArea:~,-1%.%TriangleArea:~-1% %TriangleAreaUnit%.
pause >nul
goto :EOF

我在这里所做的只是将所有内容都乘以 10,因此除以 2 不再产生会丢失的小数部分;之后,通过字符串操作插入小数分隔符。
这仅在输入数字为整数时有效。当然你可以把它扩展到更多的小数;但您需要注意中间结果不会超过带符号的 32 位限制。