Abhi's Number Abbreviation System

Abhi’s Number Abbreviation System

The best, most compact, and most efficient abbreviation system that you’ll ever find!

Hot fix (must read)

New hot fix released on Aug 1, 2020, 5:20 pm. If you are still using an old version, please switch to the new one! V 2.2V 2.3

About

In games, we generally need to cram more and more information in lesser, and lesser space. In terms of numbers, we often have to crunch numbers like 2000 to 2K. This can be done using loops and other recursive systems. But what are you didn’t? Meet Abhi’s Number Abbreviation System! This system uses clever techniques that come to my mind at 12 in the night, because that is how I work.

How it works

It’s simple

  • Break a number into 3s
    • 123456123 + 456
  • Take the foremost part
    • 123 + 456123
  • Concate it with the appropriate multiplier
    • 123123K
  • Bam, you are done!

Source Code (must read)

local suffixes = {"", "K", "M", "B", "T", "QA", "QI", "SX", "SP", "OC", "NO", "DC", "UD", "DD", "TD", "QAD", "QID", "SXD", "SPD", "OCD", "NOD", "VG", "UVG"}
function abbreviate(n)
    -- Abhi's Number Abbreviation System V 2.4
    local s = tostring(math.floor(n))
    return string.sub(s, 1, ((#s - 1) % 3) + 1) .. (suffixes)[math.floor((#s - 1) / 3) + 1]
end

Speed and reliability (must read)

The script usually takes about 0.4 to 0.05 milliseconds to run. The script will be able to handle numbers up to math.huge, but limits itself to (10 ^ (23 * 3)) - 1. That is a 1, followed by 69 zeros (it’s funny, now laugh). The script is reliable even with decimals as it just rounds down.

Distribution (must read)

Use it in your games, no need to credit me in your source code. But if you are sharing this through media means, please do credit.

Use

This script is recomended to be used in leaderboards, and other areas in GUIs.

Notes

This doesn’t round up, meaning 123999 will become 123K and will not be rounded up to 124K. Also, the output is a string, if you haven’t realized that yet :grin:.

55 Likes

May you add details about how quickly this function executes.

1 Like

IIRC all numbers in Lua 5.1/2 are doubles anyway. 5.3 has real integers, but I don’t know if Roblox adapted this from 5.3 because they take different features from different versions.

edit: fixed typo lol

1 Like

Aha I love it how you get your great ideas at 12 in the night.

Your number abbreiviation system is more understandable to me compared to other ones I seen on dev forum. Although I’m still learning string.sub I think you made me understand great programming concepts by just 1 line of code.

I’ll be looking forward to your future Ressources and I’ll be bookmarking this for whenever I’ll need it or whenever it comes up in a discussion with a friend. :wink:

2 Likes

This will be great for my cafe that I’m planning on implementing a points and coins system into. Thanks a lot!

May I ask, why this over other libraries like International or MoneyLib, what does this offer that the others don’t?

I’d point out that:

  • 1000 returns 1M and 1 returns 1K.
  • 1.23456789 returns 1T
  • Any string can be inputted and 10 ^ 15 returns 1eM.
  • tostring is redundant for numbers as that is implicitally converted to string when doing anything with it.
  • If you input any value higher with the length too high, it’ll throw an error and not fallback to the last one with more digits added.

So may I ask what makes this the best abbreviation system? I have high standards for number formatting and the flaws I pointed out aren’t acceptable.

I’d also like point out the issue using this system:

  • It also counts for decimals.
  • Numbers are converted to string as scientific notation in Lua, so 1e+15 is counted as 5.
  • It doesn’t check for non-numeric strings, so I can insert nil or a boolean, etc.

Overall the system works well if the value is checked and converted to the appropriate value otherwise I end up getting the length too high (5 for 1.2345) or too low (10 ^ 151e+15 → length of 5).

7 Likes

Hmm… That’s interesting, that’s never happened ever. Let me release a hot fix soon :slight_smile:

1 Like

Shouldn’t this be in #resources:community-resources instead of tutorials?

3 Likes