is_numeric
bool is_numeric ( string numstring )
Returns true if given numstring is numeric. A string is considered to
be numeric when it can be converted using real() function without any errors.
Spaces in the beginning and end of the string are ignored. The number may
begin with either minus or plus sign. The number itself may consist of any
numbers with decimal dot in the middle and later followed by exponential
part (which may also begin with minus or plus sign). An empty string is
considered numeric, since it will return 0 with real(). Also, if decimal
dot is not preceeded/followed by any numbers, it is considered to be
preceeded/followed by 0. Exponential part does not necessarily have to
contain numbers either. For example, "+.e" is a valid number, but "+e"
is not, because it is not considered to have any numbers.
Example 1. using is_numeric
{
if (is_numeric("-123.442"))
{
show_message("It's number");
}
else
{
show_message("Not a number");
}
if (is_numeric("-234-44"))
{
show_message("It's number");
}
else
{
show_message("Not a number");
}
}
Shows messages:
It's number
Not a number
{
var realstring, i, efound;
if (is_real(argument0))
{
return true;
}
realstring = string_lower(argument0);
while (string_char_at(realstring, 1) == " ")
{
realstring = string_delete(realstring, 1, 1);
}
for (i = string_length(realstring); string_char_at(realstring, i) == " "; i -= 1)
{
realstring = string_delete(realstring, i, 1);
}
if (realstring == "")
{
return true;
}
if (string_char_at(realstring, 1) == "-" || string_char_at(realstring, 1) == "+")
{
realstring = string_delete(realstring, 1, 1);
}
if (realstring == "" || string_char_at(realstring, 1) == "e")
{
return false;
}
efound = string_pos('e-', realstring) > 0 || string_pos('e+', realstring) > 0;
for (i = 0; i < 10; i += 1)
{
realstring = string_replace_all(realstring, chr(48 + i), "");
}
if (string_char_at(realstring, 1) == ".")
{
realstring = string_delete(realstring, 1, 1);
}
if (string_char_at(realstring, 1) == "e")
{
realstring = string_delete(realstring, 1, 1);
if (efound)
{
realstring = string_delete(realstring, 1, 1);
}
}
return realstring == "";
}