Blockland Forums > Modification Help
Strip Trailing Zeros
<< < (3/4) > >>
Destiny/Zack0Wack0:
EDIT: ignore what I said. I'm not doing too good today, lol. OP wants an isNumber thing not an isInt. Yours works fine for integers.
Greek2me:
Well to answer the original strip trailing zeros question:


--- Code: ---function stripTrailingZeros(%flag)
{
if(!isInt(%flag))
return %flag;

%pos = -1;
for(%i=0; %i < strLen(%flag); %i++)
{
%char = getSubStr(%flag,%i,1);

if(%char $= ".")
{
%dec = 1;
%pos = %i;
%zero = 1;
continue;
}

if(%char $= "0" && %dec)
{
if(!%zero)
%pos = %i;
%zero = 1;
}
else
{
%zero = 0;
%pos = -1;
}
}

if(%pos > 0)
%flag = getSubStr(%flag,0,%pos);

return %flag;
}
--- End code ---
Destiny/Zack0Wack0:
You know what would be awesome - Badspot wrapping a RegExp object into TorqueScript. Then we could do these sort of things instantly. He wouldn't really need to do much work if he used the millions of already existing C++ regex implementations.
Ipquarx:
This should do it all in 1 line, but it forgets up with decimal places for some reason, even if i set it to not strip the . char it still says false, and it also doesn't work for excessively large numbers like 1000000000000000, but I have no idea why, it shouldn't screw up like that.


--- Code: ---function isValidInteger(%i)
{
return stripChars(%i, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=[];',/{}:<>?" SPC getSubStr(%i, 0, 1) == "-" ? "" : "-") == %i && !strstr(%i, ".") < 1;
}
--- End code ---
Red_Guy:

--- Quote from: Destiny/Zack0Wack0 on November 11, 2011, 11:54:34 PM ---Also, this:


--- Code: ---function typeOf(%value)
{
if(isObject(%value))
return "object";

%dot = strPos(%value,".");

if(%dot == -1 && %value $= mFloatLength(%value,0))
return "integer";
if(%dot != -1 && %value * 1 $= mFloatLength(%value,strLen(%value) - %dot - 1))
return "float";

return "string";
}

--- End code ---


--- End quote ---

try this with your function:
echo( typeOf(12) );

gives back "object" instead of "integer"
Navigation
Message Index
Next page
Previous page

Go to full version