-2

我目前正在使用批处理脚本开发一款游戏,在一个地方,我需要进行小数的乘法运算。问题是,最终结果始终为 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 研究,并意识到在编码中整数和浮点数之间存在差异......感谢您当时帮助我!

4

5 回答 5

2

SET /A 命令的算术运算只能管理整数。想象一下,您有一个没有小数点键的计算器。你怎么能实现这个操作:150*0.84?好吧,如果您知道第二个值总是小于 1,并且有两位小数,您可以执行 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)并实现所有适当的转换!

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

于 2013-07-18T19:16:44.200 回答
2

我知道这是一个较老的问题,但我有一个类似的问题提出了我自己的一些脚本。也许我的回答仍然可以帮助有相同/相似问题的人。我对自己的问题是,“如何在我的批处理脚本中使用浮点十进制数?” 在对 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!/!multiplier! 无论您想要多少小数位到此处找到的示例脚本:https ://stackoverflow.com/a/20531384/2464491

我希望这对尝试在批处理脚本中使用浮点数时遇到相同问题的任何人有所帮助。当然,它不是为使用此类数字而设计的,但您始终可以编写脚本来解决问题。

于 2015-12-03T11:44:56.377 回答
1

批处理数学是整数,因此 0.84 将被解释为 0 或无效数字。

于 2013-07-18T15:44:25.257 回答
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"
于 2013-07-18T23:28:21.190 回答
1

您可以使用此答案中所述的混合 Batch-JScript 文件:寻找一种在 DOS 批处理文件中计算对数的方法

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

@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

于 2013-07-19T06:48:33.393 回答