phpInDev

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

Code for is_numeric.gml

{
    var realstring, i, efound;
    
    // Yeah, reals pass just fine
    if (is_real(argument0))
    {
        return true;
    }
    
    // I just don't like editing the argument, okay?
    realstring = string_lower(argument0);
    
    // Spaces in end and beginning don't matter
    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);
    }
    
    // Empty strings are just fine
    if (realstring == "")
    {
        return true;
    }
      
    // Check for plus & minus sign    
    if (string_char_at(realstring, 1) == "-" || string_char_at(realstring, 1) == "+")
    {
        realstring = string_delete(realstring, 1, 1);
    }
    
    // can't happen if we were only +/- sign or we start with e
    if (realstring == "" || string_char_at(realstring, 1) == "e")
    {
        return false;
    }
    
    // Check if we have exponential part with + or - in the beginning
    efound = string_pos('e-', realstring) > 0 || string_pos('e+', realstring) > 0;
    
    // Take away numbers
    for (i = 0; i < 10; i += 1)
    {
        realstring = string_replace_all(realstring, chr(48 + i), "");
    }
    
    // Check for decimal dot
    if (string_char_at(realstring, 1) == ".")
    {
        realstring = string_delete(realstring, 1, 1);
    }
    
    // Check for exponential part
    if (string_char_at(realstring, 1) == "e")
    {
        realstring = string_delete(realstring, 1, 1);
        
        // If we had e-/e+, del one char. Doesn't matter if it's right or not.
        if (efound)
        {
            realstring = string_delete(realstring, 1, 1);
        }
    }
        
    // It all depends on whether the lengths is now zero or not.
    return realstring == "";
}