Bradley Pouncey
Bradley Pouncey

Reputation: 25

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

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 find the area of a triangle.

I have written the code so that it asks you the base, height, and the units of the triangle. As you should know, the formula is simply, base x height / 2. So I tested the code to see if it works. But I noticed that when it divides an odd number by 2, it doesn't give a decimal answer. It instead rounds down a number. (Ex. 15/2=7)

Here is my 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% / 2
    echo The area of the triangle is %TriangleArea% %TriangleAreaUnit%.
    pause >nul
    goto :EOF

It doesn't give me any error message and everything seems to be running smoothly, I just can't figure out as to why it won't give me a decimal answer.

Upvotes: 1

Views: 1111

Answers (4)

aschipfl
aschipfl

Reputation: 34929

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.

Upvotes: 0

Hackoo
Hackoo

Reputation: 18837

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

Upvotes: 2

user6017774
user6017774

Reputation:

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

Upvotes: 1

SomethingDark
SomethingDark

Reputation: 14340

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.

Upvotes: 2

Related Questions