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

前端 未结 4 653
名媛妹妹
名媛妹妹 2021-01-15 22:10

I am making a batch file so that I just tell it what kind of formula I need to use and it tells me what variables I need to input. Right now, I am coding it so that it will

相关标签:
4条回答
  • 2021-01-15 22:24

    Use this one line basic script

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

    To use

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

    How to use

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

    The second line is how to use the program you just wrote as a command prompt command. cscript //nologo "c:\somefolder\script.vbs" "%TriangleB% * %TriangleH% / 2". Note the expression must be enclosed with quotes if it contains spaces. To put into a command prompt variable use for command. for /f "delims=" %%A in ('cscript //nologo c:\somefolder\script.vbs "%TriangleB% * %TriangleH% / 2"') Do Set Result=%%A

    So

    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
    

    Remember in a batch use %%A and interactively (ie typing) use %A

    To do the same thing in an HTA (a web page renamed to .hta which makes it act like a program)

    <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>
    

    And in vbscript only

     Wscript.echo InputBox("Height") * Inputbox("width") /2
    
    0 讨论(0)
  • 2021-01-15 22:36

    For the task at hand you could work around the limitation by some kind of fixed-point arithmetics as in the followinf sample code:

    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
    

    What I am doing here is nothing but multiplying everything with 10, so the division by 2 does no longer procude a fractional part that would be lost; afterwards, the decimal separator is inserted by string manipulation operations.
    This works only if the input numbers are integers. Of course you can extend that to more decimal figures; but you need to regard that the interim result does not exceed the signed 32-bit limitation.

    0 讨论(0)
  • 2021-01-15 22:38

    You can try it like that with a batch file :

    @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
    
    0 讨论(0)
  • 2021-01-15 22:40

    Batch math can only handle 32-bit integers, but there are ways around this limitation. The easiest way is to use PowerShell and take advantage of for /f letting you store the output of commands in variables.

    @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
    

    As long as you're using a version of Windows older than XP, this will work.

    0 讨论(0)
提交回复
热议问题