• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

separateNumber(number)

AGS

DeathSouls Owner
Joined
Oct 29, 2007
Messages
400
Reaction score
10
Location
Mexico
This function separates numbers with commas, for example:
separateNumber(123456) returns "123,456".
This makes numbers easier to read, and can be used in bank npcs, !exp commands, or anything.

Add this to function.lua
PHP:
function separateNumber(number)
	if tonumber(number) ~= nil then
		local number = tostring(number)
		local newnumber = ""
		local value = {}
		local i = 1
		if #number > 3 then
			while #number > 3 do
			value[i] = string.sub(number,-3,-1)
			number = string.sub(number,1,-4)
			i=i+1
			end
			for i=#value,1,-1 do
				newnumber = ""..newnumber..","..value[i]..""
			end
			return ""..number..""..newnumber..""
		end
		return number
	end
	return false
end

There's just two bugs I couldn't fix:
1.- If you use decimals, the result gets bugged.
2.- Numbers with 15 digits or more don't work.
This happens even when printing directly a number with 15 digits, like:
PHP:
print(123456789012345)
>1.2345678901235e+014
But it may be because of Brain Damage, the program I used to test it, maybe on OTs it works, I will test it tomorrow.

Hope someone can fix those bugs. And I know it can be made shorter :p.
 
Why all those things?

PHP:
function number.format(n)
    local left, num, right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
    return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
 
Yeah, it was some kind of sarcasm, I even gave you rep and thanked you for making it shorter.
 
I want to learn how to script like you guys...
You do it like its "Reading a book" its so easy.
You see something wrong, and bam instant fix, and it works.

Good luck to me :(
 
Justin, the best way is to experiment with it, it's basically pure English.

If you would like do something like this:
if something is equal to something else then do something
// this is what should happen
stop happening here


It would look like this:
if value == 2 then
// do something
end


Don't deny to use the functions list, it's pretty easy actually. cid means creature id, uid means unique id etc. It's pretty much English combined with some basic OT/general knowledge.

Now to become any good in scripting you shouldn't have any limitations. Go ahead, try things, start simple but pass over to more advanced scripts by time.


EDIT: Remember to look at other scripts, but don't copy it more like take a quick look at it.
 
Why all those things?

PHP:
function number.format(n)
    local left, num, right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
    return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

You forgot:
PHP:
local number = {}
number.__index = number

And some "shortening"
PHP:
function number.format(n)
    local left, num, right = n:match('^(%D*%d)(%d*)(.-)$')
    return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

Even more "shorter" would be smaller var names xD Why? No clue just funny :)


Btw this remind me of TFS isNumber function I had to fix... But that one was ofcourse worse, as the solution was way easier ^^

Smart with the reverse, wouldn't have thought of that o_O But well, you always learn new things ;)
 
I was just wondering if someone knows how to make this for php?
 
@Topic
When I asked the reason of hating me I was beeing sarcasm too. There is an expression of that in portuguese, since I am not good with American culture, i can't reproduce that here, haha :p
Here will call that "cu doce".

You forgot:
PHP:
local number = {}
number.__index = number

And some "shortening"
PHP:
function number.format(n)
    local left, num, right = n:match('^(%D*%d)(%d*)(.-)$')
    return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

Even more "shorter" would be smaller var names xD Why? No clue just funny :)


Btw this remind me of TFS isNumber function I had to fix... But that one was ofcourse worse, as the solution was way easier ^^

Smart with the reverse, wouldn't have thought of that o_O But well, you always learn new things ;)


Well, i didn't forgot anything, the reason that i called the function like it is with a dot doesn't really makes that as a class. The best way of doing a class for that is making a inheritance into string or other generic lua lib. It was only a design purposes, haha :p

Well, smaller vars will not make the code more faster, the system will allocate the same space to all of then if they had the same value. I prefer auto-explicative vars, and please, do not abuse of using syntax suggars, can be bad if you have a high pleasure (haha, nice joke :D)

Btw, reverse is available in a lot of languages (well, i saw in Phython and Ruby only xD)
 
21 lines, compared with your 4 lines,makes me feel bad :(

At least I tried :p
 
@Topic
When I asked the reason of hating me I was beeing sarcasm too. There is an expression of that in portuguese, since I am not good with American culture, i can't reproduce that here, haha :p
Here will call that "cu doce".




Well, i didn't forgot anything, the reason that i called the function like it is with a dot doesn't really makes that as a class. The best way of doing a class for that is making a inheritance into string or other generic lua lib. It was only a design purposes, haha :p

Well, smaller vars will not make the code more faster, the system will allocate the same space to all of then if they had the same value. I prefer auto-explicative vars, and please, do not abuse of using syntax suggars, can be bad if you have a high pleasure (haha, nice joke :D)

Btw, reverse is available in a lot of languages (well, i saw in Phython and Ruby only xD)

And about the class I just made it fast because otherwise it'd give him errors and well most people aren't smart enough to just change funct name to numberFormat or such x)

Making short codes with vars ppl don't understand make noobs thing it's cooler I've learnt hueahuea xD

Reverse is available in PHP too, but the way u used is was smart :p
 
Back
Top