我知道这是一个较老的问题,但我有一个类似的问题提出了我自己的一些脚本。也许我的回答仍然可以帮助有相同/相似问题的人。我对自己的问题是,“如何在我的批处理脚本中使用浮点十进制数?” 在对 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
我希望这对尝试在批处理脚本中使用浮点数时遇到相同问题的任何人有所帮助。当然,它不是为使用此类数字而设计的,但您始终可以编写脚本来解决问题。