I need some help with Abbreviations system

Ok, so i have Abbreviations system that need to work like that
1000 =1k
1010 = 1.01k
1100 = 1.1k
etc…

and it has some sort of problem
The problem is that when i try to print numbers like 1001 it will be 1.00k and not 1k

this is the code.

function Abbreviate(value)
	local abbreviations = {
		"K", -- 4 digits 
		"M", -- 7 eigits
		"B", -- 10 digits
		"T", -- 13 digits
		"Q", -- 16 digits
		"Qi", -- 19 digits
		"S", -- 22 digits
		"Sx", -- 25 digits
		"Sp", -- 25 digits
		"Oc", -- 28 digits
		"N", -- 31 digits
		"De", -- 34 digits
		"Un", -- 37 digits
		"Tr", -- 40 digits
	}
	local newstring = ""
	if value >= 1e+3 and value < 1e+6 then
		local args = string.split(tostring(value / 1e+3), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[1]
	end
	if value >= 1e+6 and value < 1e+9 then
		local args = string.split(tostring(value / 1e+6), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[2]
	end
	if value >= 1e+9 and value < 1e+12 then
		local args = string.split(tostring(value / 1e+9), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[3]
	end
	if value >= 1e+12 and value < 1e+15 then
		local args = string.split(tostring(value / 1e+12), "")
		local isperiod = false
		local ready = false
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[4]
	end
	if value >= 1e+15 and value < 1e+18 then
		local args = string.split(tostring(value / 1e+15), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[5]
	end
	if value >= 1e+18 and value < 1e+21 then
		local args = string.split(tostring(value / 1e+18), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[6]
	end
	if value >= 1e+21 and value < 1e+24 then
		local args = string.split(tostring(value / 1e+21), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[7]
	end
	if value >= 1e+24 and value < 1e+27 then
		local args = string.split(tostring(value / 1e+24), "")
		local isperiod = false
		local ready = false
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[8]
	end
	if value >= 1e+27 and value < 1e+30 then
		local args = string.split(tostring(value / 1e+27), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[9]
	end
	if value >= 1e+30 and value < 1e+33 then
		local args = string.split(tostring(value / 1e+30), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[10]
	end
	if value >= 1e+33 and value < 1e+36 then
		local args = string.split(tostring(value / 1e+30), "")
		local isperiod = false
		local ready = false
		local nextnum
		for i,v in pairs(args) do
			if not ready then
				if isperiod then
					newstring ..= v
					if not nextnum then
						nextnum = true
					else
						ready = true
					end
				else
					newstring ..= v
					if v == "." then
						isperiod = true
					end
				end
			end
		end
		newstring ..= abbreviations[11]
	end
	if string.len(newstring) == 0 then
		newstring = tostring(value)
	end
	return newstring
end

if someone can help me in a way that i would learn it will be helpfull :stuck_out_tongue:

Use this code I have given below so that it may help you

you posted this already.

but good luck :wink:

1 Like

I posted something else, before i had another system that didnt work really well, not i got just a bit of a problem here :stuck_out_tongue:

1 Like

use this
newstring = string.gsub(newstring, “.00”, “”)

1 Like

where i need to put it?
Btw really thank you

1 Like

I would improve my answer:

find line
return newstring

just before it insert
newstring = string.gsub(newstring, “.00”, “”)

Yeah so here is my Code:

Local abbreviation = { -- can be changed according to how you want it
K = 4
M = 7
B = 10
Qd = 13
Qn = 16
Sx = 19
Sp = 22
Oc = 25
N = 28
De = 31
}
local text = tostring (math.floor(number))
local ChosenAbbrevation = “ ”
for Abbrevation,digits in pairs(Abbrevations) do 
if #text >= digits and #text <(digits+3) then 
ChosenAbbrevation = Abbrevation
Break
If ChosenAbbrevation then
Local digits = abbrevations[ChosenAbbrevation]
Local rounded = math.floor(number/10^(digits-2))*10 ^ (digits – 2)
text = string.format(“%.2f,rounded/10^(digits – 1)) .. ChosenAbbrevation -- change the .2f to any number depending upon how many decimal points you want
else
text = number
end

Hope it helps :smiley:

3 Likes

please like my post :slight_smile:

Worked Thank you!!! :slight_smile:
Thx for helping me

1 Like

Wait, i just saw that its printing me things like only N or only Sx or only Oc sometimes
so its not working… :confused:

when i try to print this number
print(Abbreviate(1000000000000000000000000000000000))
in the output this is 0De… which dont need to be like that

Because 1000000000000000000000000000000000 is 35 chrs long, which is above your limit; yours is 31.

Oh ok, yea i saw that.
its now fixed lemme test :stuck_out_tongue:

Thank you so much!!!

1 Like

You can mark it a solution so we don’t get this topic in our suggested topics.

1 Like