Help with a function

I’m trying to make a function that reverses this number. I’m making a simulator and in the leaderboard they will be displayed with an M, K, and more. But I want to write a module to get this number back. Say I put 10,000,000 into a function. I would get 10M back. But what If I put 10M into a function, is it possible to get 10,000,000 as an integer back?

I wrote a function to transform the numbers:

local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}

local function convertNumber(n)
	if not tonumber(n) then return n end
	if n < 10000 then return math.floor(n) end
	local d = math.floor(math.log10(n)/3)*3
	local s = tostring(n/(10^d)):sub(1,5)
	return s.." "..tostring(T[math.floor(d/3)])
end

So, is it possible to reverse this?

Just write a table that shows the conversion like this.

local t = {K=1000,M=1000000}

local function convert(s)
    local v = tonumber(s)
    if v then return v end
    local l = s:find'%a'
    return tonumber(s:sub(1,l-1)) * t[s:sub(l,l)]
end

Also unrelated but your convertNumber function is rather inefficient.

2 Likes

What do you mean it’s inefficient? How should I do it then?

Sorry used the wrong term I meant insecure as the number MIGHT get too big and it will return unwanted value
like 100 nil

Is there any way I could stop this from happening then?

One way to prevent this is by using scientific notations or what other games have done, which is the use of alphabetical words like aa, ab, ac, ba, aaa
as they can be infinitely expanded upon there is no worry for them to return nil