我正在尝试创建一个需要整数的脚本,我需要能够检查该数字是否为整数。
答案 0 :(得分:1)
您无需将示例保留在顶部;他们只是在那里向您展示如何使用该功能。此外,它是模块化的,因此您可以将其放入代码中,除非您已经使用了这些变量名称,否则它将起作用。
@echo off
cls
:: This is a positive integer and will return TRUE
Call :isNum 54
echo %retval%
:: This is a negative integer and will return TRUE
Call :isNum -13
echo %retval%
:: This is not an integer and will return FALSE
Call :isNum 15a7
echo %retval%
:: This is a decimal and will return FALSE
Call :isNum 12.7
echo %retval%
exit /b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Determines if a string argument is an integer value
::
:: Arguments: %1 - the string to check
:: Returns: TRUE if the string is an integer, FALSE otherwise
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:isNum
setlocal enabledelayedexpansion
set num=%1
:: Strip the leading negative symbol if there is one
if "!num:~0,1!"=="-" set num=!num:~1!
:: Use all digits as delimiters. If there is anything left, it's not an integer.
for /f "delims=1234567890" %%A in ("%num%") do (
set new_num=%%A
)
set is_num=TRUE
if not "!new_num!"=="" set is_num=FALSE
endlocal&set retval=%is_num%
答案 1 :(得分:0)
此脚本利用了speed^2
:
EXIT /B
如果是整数,则返回@echo off
setlocal EnableDelayedExpansion
set /p x=
call :isint %%x%%
echo %errorlevel%
pause
exit /b
:isint
set "arg=%~1"
if NOT "!arg:?=!" == "!arg!" exit /b 1
cmd /c exit /b !arg!
if NOT "%ERRORLEVEL%" == "!arg!" exit /b 1
exit /b 0
为0,否则返回1。
甚至适用于ERRORLEVEL
,"
和&
。
int32 的定义是从/?
到2^31-1
的任何值。 (-2^31
将会溢出)