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

前端 未结 4 654
名媛妹妹
名媛妹妹 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: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.

提交回复
热议问题