StringToInt

From NWN Lexicon
Jump to: navigation, search



StringToInt(string)

Converts a string to an integer.

int StringToInt(
    string sNumber
);

Parameters

sNumber

The string to convert into an integer.


Description

Returns the value of sNumber converted to an integer.
Returns 0 on error.



Remarks

One place this would be useful is to do something based on what is heard when an NPC is listening.

It is impossible, without doing some extra checks, to determine if a return value of 0 comes from an error or from the string "0". An example of how to check it is listed below.


Version

1.61

Example

//Function to check if a string is just an integer number
 
int GetStringIsIntegerNumber(string sString)
{
int nString=StringToInt(sString);
 
//any value other than 0 means the string is just an integer number
if (nString!=0) return TRUE;
 
//Check if the string representation of nString is the same as sString.
//If it is, this will have happened:
//"0" converted to 0, converted back to "0"
//An example of 0 produced by an error:
//"bob" converted to 0, converted to "0". "bob" != "0"
string sCompare=IntToString(nString);
 
return sCompare==sString;
}

See Also

functions: 

StringToFloat



 author: Charles Feduke, editor: Lilac Soul