Page 1 of 1
Checking a numeric variable
Posted: 13 Oct 2012 15:04
by Marco
Hi all,
I need to check if a variable is entirely made of digits, i.e. if it is a number. After that, I want to know if it is positive. Basically, I want it to be a positive number.
The last part is easy, since
is all that is needed.
But what about the first part? According to Help
Code: Select all
If you compare two numerical strings, they are compared as integers:
msg ("2" < "10"); // -> true! (both parts are numeric)
msg ("2" < "10a"); // -> false! (only one parts numeric)
msg ("2a" < "10a"); // -> false! (both parts are non-numeric)
so I thought that
would have killed two birds with a stone, returning true for every positive number. That's not true instead, because if you run
Code: Select all
$delay = "bar";
msg ("$delay" > "0");
you end up with a true. What I'm doing wrong?
Re: Checking a numeric variable
Posted: 13 Oct 2012 15:29
by FluxTorpedoe
Hi'
Well, if you're not too allergic to regexes:
Code: Select all
$foo = regexreplace($foo, "\d", "");
msg ($foo == "");
Though there are certainly much proper ways to know that... (but I'm lazy).

Re: Checking a numeric variable
Posted: 13 Oct 2012 15:36
by FluxTorpedoe
Simpler:
Code: Select all
$delay = "bar";
msg ($delay*$delay > "0");
No quotes for the vars.
Edit: Doesn't work.
If a var starts with number(s), it dismisses the rest, e.g.
5a * 5a = 25
Re: Checking a numeric variable
Posted: 13 Oct 2012 15:47
by Marco
FluxTorpedoe wrote:Hi'
Well, if you're not too allergic to regexes:
Code: Select all
$foo = regexreplace($foo, "\d", "");
msg ($foo == "");
Though there are certainly much proper ways to know that... (but I'm lazy).

Regex is my second name

And was my first approach for this problem. But I was looking for a more elegant solution...
FluxTorpedoe wrote:Simpler:
Code: Select all
$delay = "bar";
msg ($delay*$delay > "0");
No quotes for the vars.
Aw, brutal yet working! Will probably resort to that... Meanwhile, thanks!
Edit:
D'oh, you're right...
Re: Checking a numeric variable
Posted: 13 Oct 2012 16:38
by FluxTorpedoe
Hi' Marco Regex
You probably already have your own solution, but I just couldn't let it go...

So to redeem myself here's one that's still not elegant but at least is a one-pass:
Code: Select all
$delay = "bar";
msg (regexreplace($delay, "(.*[a-zA-Z].*)", "-1") > 0);
(ofc we could use \D instead of [a-zA-Z] if we were sure the var doesn't start with + and/or contains other signs)
Re: Checking a numeric variable
Posted: 13 Oct 2012 17:31
by Marco
Solved with regex!
Code: Select all
$delay = "foo";
if (regexreplace("$delay", "[^\+\-\d]") == "$delay" AND "$delay" > 0) {
msg "$delay is a positive number!";
} else {
msg "$delay is... something else.";
};
The first strips out whatever is not a plus, a minus, or a digit, and compares with the original variable. Only a true number (made of digits and sign only) leads TRUE. (for the braves against the signs ,"\D")
The second checks if the variable is greater than zero. Only a positive number or a string lead TRUE.
The intersection gives me only positive numbers -> I'm happy

Anyway, thanks FluxTorpedoe for the interest!