**Updated Post: January 2nd, 2021**

@VitalWinter made a pretty good suggestion, so I’ve rewritten the code to introduce this and made some additional improvements:

```
local suffixes = {"K", "M", "B", "T", "Q"} -- numbers don't go higher than 'Q' in Lua.
local function toSuffixString(n)
local i = math.floor(math.log(n, 1e3))
local v = math.pow(10, i * 3)
return ("%.1f"):format(n / v):gsub("%.?0+$", "") .. (suffixes[i] or "")
end
```

Then, if you want to convert back:

```
local function fromSuffixString(s)
local n, suffix = string.match(s, "(.*)(%a)$")
if n and suffix then
local i = table.find(suffixes, suffix) or 0
return tonumber(n) * math.pow(10, i * 3)
end
return tonumber(s)
end
```

##
Original Post: September 12th, 2018

Personally I would do the following for converting a number to a string with a suffix.

```
local suffixes = {"K", "M", "B", "T", "Q", "Qu", "S", "Se", "O", "N", "D"}
local function toSuffixString(n)
for i = #suffixes, 1, -1 do
local v = math.pow(10, i * 3)
if n >= v then
return ("%.0f"):format(n / v) .. suffixes[i]
end
end
return tostring(n)
end
```

While to do the reverse you could do something such as

```
local function fromSuffixString(s)
local v, suffix = s:match("(%d+)(.*)")
for i = 1, #suffixes do
if suffixes[i] == suffix then
return tonumber(v) * math.pow(10, i * 3)
end
end
return tonumber(v)
end
```