Checking if a variable is a number
#3

Use this stock:
pawn Код:
stock isNumeric(const string[])
{
 new length=strlen(string);
 if (length==0) return false;
 for (new i = 0; i < length; i++)
  {
   if (
      (string[i] > '9' || string[i] < '0' && string[i]!='-' && string[i]!='+') // Not a number,'+' or '-'
       || (string[i]=='-' && i!=0)                       // A '-' but not at first.
       || (string[i]=='+' && i!=0)                       // A '+' but not at first.
     ) return false;
  }
 if (length==1 && (string[0]=='-' || string[0]=='+')) return false;
 return true;
}
Or better, I suggest using sscanf to check for the formats.

pawn Код:
//IsNumeric Example:
new value[32] = "5748";
if( IsNumeric(value) ) return print( "Yes" );
Reply


Messages In This Thread
Checking if a variable is a number - by slymatt - 25.07.2011, 17:19
Re: Checking if a variable is a number - by Skylar Paul - 25.07.2011, 17:22
Re: Checking if a variable is a number - by iPLEOMAX - 25.07.2011, 17:45
Re: Checking if a variable is a number - by slymatt - 25.07.2011, 17:48
Re: Checking if a variable is a number - by iPLEOMAX - 25.07.2011, 17:57
Re: Checking if a variable is a number - by slymatt - 25.07.2011, 18:03
Re: Checking if a variable is a number - by Dudits - 25.07.2011, 18:18
Re: Checking if a variable is a number - by slymatt - 25.07.2011, 18:19
Re: Checking if a variable is a number - by Libra_PL - 25.07.2011, 18:47
Re: Checking if a variable is a number - by slymatt - 25.07.2011, 18:52

Forum Jump:


Users browsing this thread: 5 Guest(s)