Author Topic: Strip Trailing Zeros  (Read 1870 times)

Is there a default function to trim the trailing zeros from an integer? For example, 1.0 would become 1, 2.5300 would become 2.53, etc.

Now I know that this is really easy to do when the number is stored as an integer, but in this case it's stored as a string. Also, simply returning the integer using eval("return" SPC %int @ ";"); won't work because there's the possibility that %int could be a non-integer string since this will be used in a function that checks if something is an integer.


Please see below.
« Last Edit: November 11, 2011, 10:07:19 PM by Greek2me »

Problem solved. I was trying to make my isInt function work with numbers like 1.0 so I ended up rewriting the function.

Old:
Code: [Select]
function isInt(%flag)
{
   return (%flag $= %flag * 1);
}

New:
Code: [Select]
function isInt(%flag)
{
%check = "-.0123456789";

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

%pos = strPos(%check,%char);
if(%pos < 0)
return 0;
if(%pos == 0)
{
if(%neg)
return 0;
if(%lastPos < 2 && %lastPos >= 0)
return 0;
%neg = 1;
}
if(%pos == 1)
{
if(%dec)
return 0;
if(%lastPos < 2 && %lastPos >= 0)
return 0;
%dec = 1;
}

%lastPos = %pos;
}

return 1;
}

The function works properly, but I want to know if you see anywhere that I can make it more efficient.

Code: [Select]
function isInt(%num)
{
if(%number == %number * 1)
return 1;
return 0;
}

???

Not sure what you're trying to do, are you trying to check it's an integer (a whole number) or any number (such as floating point, 0.1, 3.14 and integers)?

For integer you can do: %string $= mFloatLength(%string,0)
For any number including floating points: %string $= %string * 1

Not sure what you're trying to do, are you trying to check it's an integer (a whole number) or any number (such as floating point, 0.1, 3.14 and integers)?

For integer you can do: %string $= mFloatLength(%string,0)
For any number including floating points: %string $= %string * 1
This is the right answer. I didn't even know about the floating point check, though -- I figure it might break if the input is "1.10000", though. Very nice work regardless.

The new function works for floating point, everything.

For any number including floating points: %string $= %string * 1
That's what I had originally but like Ichverbot (Iban?) said, that breaks when there's extra 0s at the end.

Code: [Select]
function isNumber(%value)
{
%dot = strPos(%value,".");

if(%dot == -1)
return %value $= mFloatLength(%value,0);

return %value * 1 $= mFloatLength(%value,strLen(%value) - %dot - 1);
}

EDIT: aaaaa just wait this worked before, let me fix it.
EDIT: Fixed. This is what I get for mindless copy pasting.
« Last Edit: November 11, 2011, 11:36:07 PM by Destiny/Zack0Wack0 »

It would be really kickin' rad if Badspot integrated some "isInt" / "isFloat" functionality engine-wise. Handling it manually is a massive hack.

Also, this:

Code: [Select]
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";
}

EDIT:
It would be really kickin' rad if Badspot integrated some "isInt" / "isFloat" functionality engine-wise. Handling it manually is a massive hack.
isNull would be brilliantly useful.
« Last Edit: November 11, 2011, 11:57:11 PM by Destiny/Zack0Wack0 »

I use this for isint

Code: [Select]
function isint(%x)
{
   return (%x $= mfloor(%x));
}

I have yet to come across any problems

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.
« Last Edit: November 12, 2011, 12:04:04 AM by Destiny/Zack0Wack0 »

Well to answer the original strip trailing zeros question:

Code: [Select]
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;
}

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.

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: [Select]
function isValidInteger(%i)
{
return stripChars(%i, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=[];',/{}:<>?" SPC getSubStr(%i, 0, 1) == "-" ? "" : "-") == %i && !strstr(%i, ".") < 1;
}

Also, this:

Code: [Select]
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";
}


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

gives back "object" instead of "integer"