How to make accurate number abbreviations

I’m trying to make some accurate abbreviations for my values for example.

local abbreviations = {
K = 1000,
M = 1000000,
B = 1000000000,
T = 1000000000000,
Qa = 1000000000000000,
Qi = 1000000000000000000
}

Then it shows as

4.26K
58.16K
1.73M
205.05B
155.10T
563.26Qa
7.19Qi

Please help me with this

2 Likes

The obvious approach would be to check if the absolute value of the number is between 0 and 999, then 1000 and 99999, and so on. However you can determine what power of a thousand the number is via logarithms.

Here’s an example of the latter:

local SuffixList = { "", "K", "M", "B", "T", "Q" }

local function Format(value, idp)
	local exp = math.floor(math.log(math.max(1, math.abs(value)), 1000))
	local suffix = SuffixList[1 + exp] or ("e+" .. exp)
	local norm = math.floor(value * ((10 ^ idp) / (1000 ^ exp))) / (10 ^ idp)

	return ("%." .. idp .. "f%s"):format(norm, suffix)
end
15 Likes

How would I set a gui text using this? :thinking:

Set the Text property of the TextLabel/whatever GuiObject you’re using to the return value of the Format function.

label.Text = Format(12345, 2) -- 12.34K

2 Likes

So like label.Text = Format(ds.Value,2)?

It works thank you so much! <3