Check if a variable is a number

  • Follow


Dear All,

I am having a headache about checking if a variable is a number
(floating, double, and integer. Anything that is a numerical value).

If I use

if { [string is alnum "$_var1"] != 1}

The strings like "    "  or  "           "  (all blanks) will pass the
test, giving

So, is there a simple command to check if a variable is a numerical
number ?

And, is there also a simple command to check of a string is empty, or
just full of blanks ?

Thanks for the help in advance.

regards
S-Y. Chen
0
Reply S 3/10/2010 6:20:27 PM

S-Y. Chen wrote:
> So, is there a simple command to check if a variable is a numerical
> number ?

[string is double -strict $var]

-- 
| Don Porter          Mathematical and Computational Sciences Division |
| donald.porter@nist.gov             Information Technology Laboratory |
| http://math.nist.gov/~DPorter/                                  NIST |
|______________________________________________________________________|
0
Reply Don 3/10/2010 6:26:25 PM


On Mar 10, 1:20=A0pm, "S-Y. Chen" <shenyeh_c...@hotmail.com> wrote:
> Dear All,
>
> I am having a headache about checking if a variable is a number
> (floating, double, and integer. Anything that is a numerical value).

Hi,

You can try something like if {[scan $myinput "%f%c" number
stopper]=3D=3D1} {bleh}

If $myinput is a number, scan should output 1; if it is whitespace it
should output -1; if it is nonnumerical garbage it should output 0;
and if it is garbage that begins with numerical characters, e.g.
-123abc, it should output 2.

--X
0
Reply Prof 3/10/2010 6:33:45 PM

2 Replies
1206 Views

(page loaded in 0.058 seconds)

Similiar Articles:













7/23/2012 6:23:02 AM


Reply: