Incorrect value of strToInt for negative numbers
Posted: Fri Mar 02, 2012 1:58 pm
trace(intToStr(strToInt("-1"))) displays -29, trace(intToStr(strToInt("-10"))) displays -290, etc.
The rapid development solution with a tiny footprint
http://emix8.org/forum/
Code: Select all
int str2Int(string S)
{
int R, F = 0;
int L = length(S);
for(int N=0; N<L; ++N)
{
int C = ord(subStr(S,N,1));
if(C == 45)
{
if(N != 0)return 0; else F = 1;
}
else if(C > 47 && C < 58)
{
R = R*10+C-48;
}
else return 0;
}
if(F)R = 0-R;
return R;
}
Code: Select all
result = subStr(str, 0,1) == "-" ? strToInt(subStr(str, 1, length(str)-1))*-1: strToInt(str);