ID:195028
 
/*
Title: IsNum()
Credit To: Metamorphman
Contributed By: Metamorphman

This proc returns true if the specified string is:
1.A number
or
2.A text string, containing nothing but numbers.

Useful for validating things like PIN Codes.
*/


proc/IsNum( string )

if( isnum( string ) )
return 1

if( !istext( string ) )
return

for( var/x = 1 to length( string ) )
var/currentChar = text2ascii( string , x )
if( currentChar > 57 || currentChar < 48 ) return 0

return 1
Metamorphman wrote:
/*
> Title: IsNum()
> Credit To: Metamorphman
> Contributed By: Metamorphman
>
> This proc returns true if the specified string is:
> 1.A number
> or
> 2.A text string, containing nothing but numbers.
>
> Useful for validating things like PIN Codes.
> */

>
> proc/IsNum( string )
>
> if( isnum( string ) )
> return 1
>
> if( !istext( string ) )
> return
>
> for( var/x = 1 to length( string ) )
> var/currentChar = text2ascii( string , x )
> if( currentChar > 57 || currentChar < 48 ) return 0
>
> return 1


That seems a little much. Consider:
proc/IsNum(string)
if(istext(string))
string = text2num(string)
return isnum(string)
In response to Kuraudo
As far as robustness and general utility goes, that solution works worse than his. The check here is if the text string is, to quote, "A text string, containing nothing but numbers. ", but text2num() will return a number even if that isn't so, as long as there's any number in the beginning of the string. Meaning your IsNum() will incorrectly return true if given a string like "1abcdefg".
In response to Kaioken
Kaioken wrote:
As far as robustness and general utility goes

I suppose you could argue that returning true if "1a" is given isn't what he wanted, but you can't argue that it's invalid in terms of robustness and general utility; I would argue that if text2num() generates a number from it, it should be considered a number. There are only a handful of cases I can think of where you would specifically need to make sure that the characters are only numeric, since most cases of using a number from a text string will involve a conversion via text2num() anyway.
In response to Kuraudo
Kuraudo wrote:
[...] you can't argue that it's invalid in terms of robustness and general utility;

Oh, it is quite so, since when you're doing the check specifically to check if a string is all-numbers, and that check returns true even if it isn't, then not only does it miss the entire point of making such a check in the first place but of course you can't rely on it.
And in the cases you mentioned that you're going to convert the string with text2num() anyway, calling an external proc to check if a number can be produced with it from the string first (not to mention one that calls text2num() itself to do so) is quite... unintelligent, since you just need to call text2num(), storing the value returned and seeing if it's a non-null one or not, then if it isn't, go on about using it. No need to use an external proc for that, and end up calling text2num() twice as well.
In response to Kaioken
Maybe we should just go with this:
proc/IsNum(string)
return isnum(string) || ("[text2num(string)]" == string) && string