Author Topic: Finding if a string is a number [Solved by Ipquarx]  (Read 2427 times)


I want to know how to find if a string is a number using a for statement because that seems the only way that can handle over a million.
nope

Code: [Select]
function isInteger(%var)
{
return stripChars(%var, "0123456789") $= "";
}

It basically removes the only characters an integer can have and returns true if there are no other characters.
« Last Edit: September 14, 2012, 01:54:24 PM by Ipquarx »

nope

Code: [Select]
function isInteger(%var)
{
return stripChars(%var, "0123456789") $= "";
}

It basically removes the only characters an integer can have and returns true if there are no other characters.
Thank you Ipquarx. It works.

Code: [Select]
function isNumber(%string)
{
if(stripChars(%string, "0123456789.") $= "")
{
return true;
}

return false;
}

isNumber("2.4.6")
isNumber(".")

While reading into this I found something interesting:
$ echo(10291059192+0);
1.70112e+009
$ echo("10291059192"+0);
1.02911e+010

loving fascinating

$ echo("2e24");
2e24
$ echo("2e24"+0);
2e+024
$ echo("2e24" > 999999);
1

help i'm so confused

Code: [Select]
function isNumeric(%i)
{
if(%i <= 999999 && %i >= -999999)
{
// Fast solution
return %i $= %i * 1;
} else {
// Slow solution
%dot = 0; // Track if period present
%sci = 0; // Track if scientific notation present
for(%j=0;%j<strLen(%i);%j++)
{
%c = getSubStr(%i,%j,1);
if(%c $= "-")
{
if(%j != 0)
return false; // Negative not the first character
continue;
}
if(%c $= ".")
{
if(%dot || %j == 0)
return false; // Multiple periods or nothing before the period
else
%dot = 1;
continue;
}
if(%c $= "e")
{
if(%sci != 0)
return false; // Multiple Es present
else
%sci = -1;
continue;
}
if(%c $= "+")
{
if(%sci != -1)
return false; // + is not part of e+ scientific notation
else
%sci = 1;
continue;
}
if(%sci == -1 || strPos("0123456789",%c) == -1)
return false; // An E was present without a + or the current character is not a number
}
if(%sci == -1)
return false; // Last character was an E. Sneaky bastard
return true; // Gamut has been run, nothing returned, ergo it is fine
}
}
For numbers in the range of -999999 to 999999, uses the fast, math-based solution. For numbers which will error out with that solution (sometimes) due to scientific notation, uses a string parser to match if it's valid scientific notation (or an integer outside that range that is still not scientific notated).

Can't find anything it matches that it shouldn't, or anything it doesn't match that it should


I blame torque

But that will flawlessly detect a valid number that Torque will interpret as such

Whereas just using stripChars that way will return true for stuff like "." and "2.4.6" (as I noted and then proceeded to drown out) which Torque won't interpret as numbers, and will also return false for negative numbers.

Let's be honest, mostly this has been a pure case of proving Ipquarx wrong because epeen. Producing a handy resource function for validating numbers of any size was just a side effect

I blame torque

But that will flawlessly detect a valid number that Torque will interpret as such

Whereas just using stripChars that way will return true for stuff like "." and "2.4.6" (as I noted and then proceeded to drown out) which Torque won't interpret as numbers, and will also return false for negative numbers.

Let's be honest, mostly this has been a pure case of proving Ipquarx wrong because epeen. Producing a handy resource function for validating numbers of any size was just a side effect
Why don't you like Ipquarx?

2.4.6 is not a number, M, It's a formatted version.

And besides, it was only meant for integers :p

Oh and by the way M, 1.4453e-025 is valid, yet your script doesnt allow it.
« Last Edit: September 15, 2012, 10:37:48 AM by Ipquarx »


2.4.6 is not a number, M, It's a formatted version.

And besides, it was only meant for integers :p

Oh and by the way M, 1.4453e-025 is valid, yet your script doesnt allow it.
I can't honestly think of a situation where you'd want to present a float so precise it actually loses its precision, in fact I'd never even encountered it on account of never dividing things by >10000
Code: [Select]
if(%c $= "-")
{
if(%sci == -1)
%sci = 1; // Negative powers
else if(%j != 0)
return false; // Negative not the first character
continue;
}
Easily fixed anyway