SA-MP Forums Archive
Checking if a variable is a number - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Checking if a variable is a number (/showthread.php?tid=271716)



Checking if a variable is a number - slymatt - 25.07.2011

how can i do a check to see if a variable is all number for example

Код:
 if(inputtext = is a number);
{
}
else return SendClientMessage(playerid, colour, you can only enter numbers sorry);



Re: Checking if a variable is a number - Skylar Paul - 25.07.2011

IsNumeric:

pawn Код:
stock IsNumeric(const string[])
{
        for (new i = 0, j = strlen(string); i < j; i++)
        {
                if (string[i] > '9' || string[i] < '0') return 0;
        }
        return 1;
}
pawn Код:
if(IsNumeric(inputtext))
{
//its a number!
}
else return 1; //not a number.



Re: Checking if a variable is a number - iPLEOMAX - 25.07.2011

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" );



Re: Checking if a variable is a number - slymatt - 25.07.2011

i now get this error:
Код:
error 033: array must be indexed (variable "inputtext")
from this line:
Код:
if(inputtext == PlayerInfo[playerid][Bankdeposit] || inputtext < PlayerInfo[playerid][Bankdeposit])



Re: Checking if a variable is a number - iPLEOMAX - 25.07.2011

After IsNumeric check, make the line this way:

pawn Код:
if(strval(inputtext) == PlayerInfo[playerid][Bankdeposit] || strval(inputtext) < PlayerInfo[playerid][Bankdeposit])
Why, because inputtext is STILL a string, right?

strcmp to compare strings, == for ints.


Re: Checking if a variable is a number - slymatt - 25.07.2011

compiled fine =P thanks


Re: Checking if a variable is a number - Dudits - 25.07.2011

Use sscanf bro.
pawn Код:
new numeric;
if(sscanf(params, "i", numeric))
{
      return 1;
}
else
{
      // your command
}



Re: Checking if a variable is a number - slymatt - 25.07.2011

so how do i do this:

Код:
GivePlayerMoney(playerid, -strval(inputtext));
i want the strval(inputtext); to take the amount in inputtext away


Re: Checking if a variable is a number - Libra_PL - 25.07.2011

Why you don't try "-12345" for example in inputtext?
(with minus)


Re: Checking if a variable is a number - slymatt - 25.07.2011

because then the input the player puts in wont be there and it will just take -12345 off .....