Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to thoroughly check if a variable contains a valid integer number?

31 views
Skip to first unread message

JJ

unread,
Oct 25, 2017, 9:36:53 AM10/25/17
to
e.g. if I try to check it like this:

@echo off
setlocal
call :check notAnumber
call :check 123
call :check 0
goto :eof
:check
set /a n=%1
if "%n%" equ "0" (echo FALSE) else (echo TRUE)

Only the first two checks shows the correct answer. The third one is
incorrect.

I've also found an answer in SO, but the code fails if the variable being
checked is empty or if the value is e.g. (no quotes) "12 34". i.e. shown as
TRUE, where it should have been FALSE.

<https://stackoverflow.com/a/33266774/1510085>

While I could make it do a simple pre-check for an empty variable. e.g.

if not "%num%" == "" (
::do interget check...
) else (
echo FALSE
)

But it seems only to add complexity of the validation process.
So, is there a simpler method? Without using any external program.
Message has been deleted

npocmaka

unread,
Oct 25, 2017, 2:36:51 PM10/25/17
to
Try this.Though some special symbols like "!" and "^" could cause problems:

:::::::::::::::::::::::
@echo off

:isInterer input [returnVar]
setlocal enableDelayedexpansion
set "input=%~1"

if "!input:~0,1!" equ "-" set "input=!input:~1!"

for %%# in (1 2 3 4 5 6 7 8 9 0) do (
if not "!input!" == "" (
set "input=!input:%%#=!"
)
)

if "!input!" equ "" (
set result=true
) else (
set result=false
)

endlocal & if "%~2" neq "" (set %~2=%result%) else echo %result%
:::::::::::::::::::::::

John Gray

unread,
Nov 3, 2017, 5:07:46 PM11/3/17
to
You might be able to extend some code which I acquired from Frank-Peter Schultze in 2008 and reworked slightly:
set variable=7692
set letters=
for /f "delims=0123456789" %%a in ("%variable%") do set letters=y
if not defined letters echo %variable% is numeric
It copes with an embedded blank in a number, but that's all the checking I've done.

Tom Del Rosso

unread,
Nov 4, 2017, 11:18:58 AM11/4/17
to
I was going to offer the same idea. Just add a check for negative
numbers by placing this before the FOR statement. Then the hyphen will
be allowed only as the first character.

if "%variable:~0,1%"=="-" set "variable=%variable:~1%"

The quotes in the SET statement ensure that no spaces sneak in. It's
recommended to use quotes on all SET statements where a space would
matter, like:

set "letters="




Dr J R Stockton

unread,
Nov 9, 2017, 5:00:16 AM11/9/17
to
On Wednesday, October 25, 2017 at 2:36:53 PM UTC+1, JJ wrote:

>How to thoroughly check if a variable contains a valid
>integer number?
> e.g. if I try to check it like this:

Firstly, define what *you* mean by "integer".

Is 1e3 an integer, value decimal 1000?
Is 077 an integer, value decimal 63?
Is 0xF an integer, value decimal 15?
Is 1.0 an integer, value 1?
Are signs + and/or - allowed?

--
(c) John Stockton, near London, UK. Using Google, no spell-check. |
Mail: J.R.""""""""@physics.org - or as Reply-To |
0 new messages