批量小数变量计算器

时间:2013-07-18 14:53:19

标签: variables batch-file decimal calculator

我目前正在使用批处理脚本进行游戏,在一个地方,我需要对小数进行乘法运算。问题是,最终结果总是为0。

这是代码:

@echo off 
echo Calcultating New Values
echo ...
ping localhost -n 2 >nul
set /p coal_price_buy_brt=<coal_price_buy_brt.wss
set /p coal_ind_buy=<coal_ind_buy.wss
cls
echo First Values :
echo ################################
echo ## Coal Price Brutto  ##  %coal_price_buy_brt%  ##
echo ################################
echo ## Coal Index Buy ## %coal_ind_buy% ##
echo ################################
ping localhost -n 3 >nul
echo %coal_price_buy_brt%
echo %coal_ind_buy%
set ENABLEDELAYEDEXPANSION=coal_price_buy_net
set /p coal_price_buy_net=<calc %coal_price_buy_brt%*%coal_ind_buy%
echo Complete Table :
echo ################################
echo ## Coal Price Brutto  ##  %coal_price_buy_brt%  ##
echo ################################
echo ## Coal Index Buy ## %coal_ind_buy% ##
echo ################################
echo ## Coal Price Netto ## %coal_price_buy_net% ##
echo ################################

文件数据是:

coal_price_buy_brt = 150
coal_ind_buy = 0.84

编辑: 在这篇文章发表4年后,我现在正在进行IT研究并意识到编码中的整数和浮点数之间存在差异...... 谢谢你帮我回来了!

5 个答案:

答案 0 :(得分:2)

我知道这是一个较老的问题,但我有一个类似的问题想出了一些我自己的脚本。也许我的回答仍然可以帮助那些有相同/类似问题的人。我自己的问题是,&#34;如何在我的批处理脚本中使用浮点十进制数?&#34;经过对StackOverflow的其他个人问题的深思熟虑和研究,我想出了以下示例脚本。它几乎将浮点数转换为两个变量形式的分数,可以在脚本的其余部分中使用。它可以与此答案https://stackoverflow.com/a/20531384/2464491一起用于类似的问题。

@echo off
setlocal EnableExtensions
setlocal EnableDelayedExpansion

REM This is how I do a block comment.
goto SOF
========Begin Comment========
Title:  deciTest.bat

This batch script checks to see if the number inputed is an interger or a floating point number.
If it is a floating point number, it determines to how many decimal places up to 4096 places.
It then informes the user of how to use the floating point number in arithmatic equations.
Of course, if you include within your script, you can simply call upon the !intOut! and
!multiplier! variables elswhere in your script.
=========End Comment=========


:SOF

REM Check to see if the user supplied a number.
if "%1"=="" (
    REM If not, tell them how to use the file.
    echo Usage: deciTest.bat [number]
    echo.
    echo [number]   The number to check.  Enter either an integer
    echo            or a floating point number.
    echo.
    goto eof
)

REM Assign the user input to variable decNum
set decNum=%1

REM Plop the number into a file
echo !decNum!>decNum.tmp

REM Check to see if there is a decimal point
findstr /c:"." decNum.tmp >null

REM If it is found, the number is a floating point number
REM So lets make it so we can use it.
if %errorlevel%==0 (
    REM Separate our Characteristic (before the .) and Mantissa (after the .)
    for /f "tokens=1-18* delims=." %%a in (decNum.tmp) do (
        REM Count the length of our Mantissa (How may decimal places?)
        set "s=%%b"
        set "s=!s!#"
        set "decPlaces=0"
        for %%P in (4096 2048 1024 512 128 64 32 16 8 4 2 1) do (
            if "!s:~%%P,1!" NEQ "" (
                set /a "decPlaces+=%%P"
                set "s=!S:~%%P!"
            )
        )
        REM Inform the user of our findings.
        echo %%a.%%b is a floating point number with !decPlaces! decimal places
        call :Integrate
        echo.
        REM Create the variable !intOUt! for use elswhere in the code
        set /a intOut=%%a*!multiple!+%%b
        REM Tell the user how to use this particular floating number
        echo Your batch file can use !intOut! in your arithmatic equations.
        echo Simply divide your result by !multiple!.
    )
) else (
    REM If it aint floatin', it's an integer
    echo %1 is an integer
)

goto eof

:Integrate  REM Create the !multiple! variable to be used elsewhere in the script
set count=!decPlaces!
set multiple=1

:startloop
    set /a multiple*=10
    set /a count-=1
    if not !count!==0 goto startloop

:eof

该代码演示了如何处理浮点数。从本质上讲,它将浮点数转换为分数(!intOut!/!multipler!)。如果你稍微调整算术。乘以!intOut!,然后发送!intOut!/!乘数!如果您想要在此处找到示例脚本,请使用多个小数位:https://stackoverflow.com/a/20531384/2464491

我希望这可以帮助任何在尝试使用批处理脚本中的浮点数时遇到同样问题的人。当然它不是设计用于这样的数字,但你总是可以编写问题的脚本。

答案 1 :(得分:1)

批次数学是INTEGER,因此0.84将被解释为0或无效数字。

答案 2 :(得分:1)

SET / A命令的算术运算只能管理整数。想象一下,你有一个没有小数点键的计算器。你怎么能实现这个操作:150 * 0.84?好吧,如果你知道第二个值总是小于一个有两位小数,你可以执行150 * 84而在结果的第二个数字(从右到左)之前插入一个小数点:

@echo off
set coal_price_buy_brt=150
set coal_ind_buy=0.84

rem Convert coal_ind_buy to integer
set coal_ind_buy=%coal_ind_buy:0.=%

rem Execute the multiplication
set /A result=coal_price_buy_brt*coal_ind_buy

echo Result as integer: %result%
echo Result as fixed point with two decimals: %result:~0,-2%.%result:~-2%

如果值可能包含整数部分,那么您可以实现适当的转换为整数值,执行乘法,并在正确的位置插入小数点;但是,总是必须选择固定数量的小数位(“定点算术”),除非您想将值转换为浮点(指数为10)并实现所有适当的转换!

有关批处理中定点算术运算的更多详细信息,请参阅:http://www.dostips.com/forum/viewtopic.php?f=3&t=2704&p=12523#p12523

答案 3 :(得分:1)

您可以调用此批处理文件进行数学评估。

将其命名为vbs.bat,然后使用call vbs 150*0.84,结果将位于名为%val%

的变量中
@echo off
>"%temp%\VBS.vbs" echo Set fso = CreateObject("Scripting.FileSystemObject") : Wscript.echo (%*)
for /f "delims=" %%a in ('cscript /nologo "%temp%\VBS.vbs"') do set "val=%%a"
del "%temp%\VBS.vbs"

答案 4 :(得分:1)

您可以使用此答案中所述的混合Batch-JScript文件:looking for a way to calculate logarithm in a DOS batch file

此方法允许您评估任何浮点运算,包括对数,平方根等。

@if (@CodeSection == @Batch) @then
@echo off

rem Evaluate floating point expressions via JScript, for example:
call :Expr result=%coal_price_buy_brt%*%coal_ind_buy%
echo %result%
goto :EOF

:Expr result=expression
for /F "delims=" %%a in ('Cscript //nologo //E:JScript "%~F0" "%2"') do set "%1=%%a"
exit /B

@end
WScript.Echo(eval(WScript.Arguments.Unnamed.Item(0)));

有关可用JScript数学运算的更多详细信息,请参阅:http://msdn.microsoft.com/en-us/library/ie/b272f386(v=vs.94).aspx