1

Simply asked, I need to check if a variable is numerical. I'm aware of the ability of:

set /a variable1=%variable%

setting non numerical strings to 0, but i need to be able to have 0 as an intiger as well as negative numbers.

This will be run very often, so a fast script is preferred. I've tried to echo the variable into a .txt, and use a for loop to scan through and return an error if anything other than 0-9 is detected, but the script is excessively long running, and frankly is a mess.

6
  • Can you edit the question to include the code you used to check if an integer was provided by the user? You were probably close. Commented Oct 21, 2015 at 18:22
  • go to google and type in: batch file is numeric - or do it in the search bar in the upper right hand corner of this page.... Commented Oct 21, 2015 at 18:29
  • 1
    Have you tried something like the for statement from this post, or the InStr method mentioned there as well? Commented Oct 21, 2015 at 18:44
  • 1
    I'm pretty sure 'set /a v=%v%' can handle negative values as well. So pretty much anything numeric will return something other than zero except zero itself. So why not just test for zero before hand and then nest this within that if statement? So the logic would go something like this... (If zero, then numeric, else (if set /a v=%v% --> zero, then non-numeric, else numeric)) Commented Oct 21, 2015 at 19:00
  • This answer to an almost identical question solves your problem. Commented Oct 22, 2015 at 1:41

3 Answers 3

3

As mentioned in question17584282

The easiest for digits should be:

IF %1 NEQ +%1 echo Notnumeric!

If negative numbers (hyphen) are also to be considered, this will work

SET number=%1
if %1 EQU +%1 echo positive number
if %1==-%number:-=% echo negative number

Learned from https://www.itprotoday.com/compute-engines/jsi-tip-9692-how-can-batch-script-determine-if-variable-or-parameter-integer

Sign up to request clarification or add additional context in comments.

Comments

2

You could do something to this affect. Remove all numbers. If anything is left over it is not an integer. Not saying this is perfect but it is a step in the right direction.

set "tempvar="
FOR /F "tokens=* delims=-0123456789" %%G IN ("%variable1%") DO SET "tempvar=%%G"

IF DEFINED tempvar echo NOT AN INTEGER

Comments

0
@echo off 

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

if "!input:~0,1!" equ "-" (
    set "input=!input:~1!"
) else (
    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% 

try this.Some special symbols like ! and ^ could cause trouble though. You can also use findstr:

@echo off 

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

if "!input:~0,1!" equ "-" (
    set "input=!input:~1!"
) else (
    if "!input:~0,1!" equ "+" set "input=!input:~1!"
)

echo !input!|findstr /r "[^0-9]" >nul 2>&1

if %errorlevel% equ 0 ( 
        set result=false 
) else ( 
        set result=true 
)
endlocal & if "%~2" neq "" (set %~2=%result%) else echo %result% 

1 Comment

I mean, this was 2 years ago but it is a nice solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.