| Blockland Forums > Modification Help |
| Strip Trailing Zeros |
| (1/4) > >> |
| Greek2me:
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. |
| 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: ---function isInt(%flag) { return (%flag $= %flag * 1); } --- End code --- New: --- Code: ---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; } --- End code --- The function works properly, but I want to know if you see anywhere that I can make it more efficient. |
| cheese6:
--- Code: ---function isInt(%num) { if(%number == %number * 1) return 1; return 0; } --- End code --- ??? |
| Destiny/Zack0Wack0:
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 |
| Ichverbot:
--- Quote from: Destiny/Zack0Wack0 on November 11, 2011, 10:35:38 PM ---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 --- End quote --- 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. |
| Navigation |
| Message Index |
| Next page |