FixNum Module | Example: 1000 returns 1K

Hey! I made this basic module to make numbers more ‘readable’, it doesn’t support numbers bigger than 1B (1000000000) and less than 1K (1000)

Get the Module here: https://create.roblox.com/marketplace/asset/12624920410/fixnum or fixnum - Roblox

Module source: (ModuleScript)

local module = {}

module.Get = function(num: number)

if num <= 1000 or num >= 1000000000 then

return tostring(num)

end

if num >= 1000 and num < 10000 then

local str = tostring(num)

if tonumber(str:sub(2,2)) >= 1 then

return str:sub(1,1).."."..str:sub(2,2).."K+"

end

if num == 1000 then

return str:sub(1,1).."K"

end

return str:sub(1,1).."K+"

elseif num >= 10000 and num < 100000 then

local str = tostring(num)

if tonumber(str:sub(3,3)) >= 1 then

return str:sub(1,2).."."..str:sub(3,3).."K+"

end

if num == 10000 then

return str:sub(1,2).."K"

end

return str:sub(1,2).."K+"

elseif num >= 100000 and num < 1000000 then

local str = tostring(num)

if tonumber(str:sub(4,4)) >= 1 then

return str:sub(1,3).."."..str:sub(4,4).."K+"

end

if num == 100000 then

return str:sub(1,3).."K"

end

return str:sub(1,3).."K+"

elseif num >= 1000000 and num < 10000000 then

local str = tostring(num)

if tonumber(str:sub(2,2)) >= 1 and tonumber(str:sub(3,3)) == 0 then

return str:sub(1,1).."."..str:sub(2,2).."M+"

end

if tonumber(str:sub(2,2)) >= 1 and tonumber(str:sub(3,3)) >= 1 then

return str:sub(1,1).."."..str:sub(2,3).."M+"

end

if num == 1000000 then

return str:sub(1,1).."M"

end

return str:sub(1,1).."M+"

elseif num >= 10000000 and num < 100000000 then

local str = tostring(num)

if tonumber(str:sub(3,3)) >= 1 and tonumber(str:sub(4,4)) == 0 then

return str:sub(1,2).."."..str:sub(3,3).."M+"

end

if tonumber(str:sub(3,3)) >= 1 and tonumber(str:sub(4,4)) >= 1 then

return str:sub(1,2).."."..str:sub(3,4).."M+"

end

if num == 10000000 then

return str:sub(1,2).."M"

end

return str:sub(1,2).."M+"

elseif num >= 100000000 and num < 1000000000 then

local str = tostring(num)

if tonumber(str:sub(4,4)) >= 1 and tonumber(str:sub(5,5)) == 0 then

return str:sub(1,3).."."..str:sub(4,4).."M+"

end

if tonumber(str:sub(4,4)) >= 1 and tonumber(str:sub(5,5)) >= 1 then

return str:sub(1,3).."."..str:sub(4,5).."M+"

end

if num == 100000000 then

return str:sub(1,3).."M"

end

return str:sub(1,3).."M+"

elseif num >= 1000000000 and num < 10000000000 then

local str = tostring(num)

if tonumber(str:sub(5,5)) >= 1 and tonumber(str:sub(6,6)) == 0 then

return str:sub(1,1).."."..str:sub(5,5).."B+"

end

if tonumber(str:sub(5,5)) >= 1 and tonumber(str:sub(6,6)) >= 1 then

return str:sub(1,1).."."..str:sub(5,6).."B+"

end

if num == 1000000000 then

return str:sub(1,1).."B"

end

return str:sub(1,1).."B+"

end

end

return module

Example usage: (LocalScript or Script)

local module = require(game:GetService("ReplicatedStorage").fixnum) -- Module path OR require(12624920410) to require the module model

print(module.Get(1000))
print(module.Get(1200))

print(module.Get(10000))
print(module.Get(12040))

print(module.Get(1000000))
print(module.Get(1204000))

print(module.Get(10000000))
print(module.Get(12340000))

print(module.Get(100000000))
print(module.Get(120400000))

print(module.Get(1000000000))
print(module.Get(1204500000))

--[[
Output:
  1K
  1.2K+
  10K
  12K+
  1M
  1.2M+
  10M
  12.34M+
  100M 
  120.4M+
  1B
  1.5B+
]]

Note: This code may not be fully optimized, this was rushed and made in less than 30 minutes. I dont mind if you want to optimize the code better, you can use it for anything you wish to.

6 Likes

Sorry for those critics, but the script itself is pretty empty. Id suggest atleast making it modular (a table of values)

1 Like

Thank you for sharing your code. I took the liberty of optimizing it further and wanted to share my version as an alternative.

local module = {}

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

module.Get = function(num: number)
	local suffixIndex = 1
	while num >= 1000 and suffixIndex <= #suffixes do
		num = num / 1000
		suffixIndex = suffixIndex + 1
	end
	local formattedValue = string.format("%.1f", num)
	if formattedValue == "1000.0" and suffixIndex < #suffixes then
		formattedValue = "1.0"
		suffixIndex = suffixIndex + 1
	end
	return formattedValue .. suffixes[suffixIndex]
end

return module
print(module.Get(123)) -- Output: 123
print(module.Get(1234)) -- Output: 1.2K
print(module.Get(12345)) -- Output: 12.3K
print(module.Get(123456)) -- Output: 123.5K
print(module.Get(1234567)) -- Output: 1.2M
print(module.Get(12345678)) -- Output: 12.3M
print(module.Get(123456789)) -- Output: 123.5M
print(module.Get(1234567890)) -- Output: 1.2B
3 Likes

You return a module with a single function, so why bother with a module? Just return a function named convert (or your choice) instead of a module (simplifying @Zhekoay’s code, but the concept is the same).

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

return function(num: number)
	local suffixIndex = 1
	while num >= 1000 and suffixIndex <= #suffixes do
		num = num / 1000
		suffixIndex = suffixIndex + 1
	end
	local formattedValue = string.format("%.1f", num)
	if formattedValue == "1000.0" and suffixIndex < #suffixes then
		formattedValue = "1.0"
		suffixIndex = suffixIndex + 1
	end
	return formattedValue .. suffixes[suffixIndex]
end
local convert = require(game:GetService("ReplicatedStorage").fixnum)

print(convert(1000)) -- Output: 1K
1 Like

Hi, actually, I know you took efforts, but with some cool trick, you can actually abbrievate a number with just a line of code.