Abbreviation Number get floated number

Hello, if i set 100_325_000_000 in my Abbreviation, is return 100T, because i set floor, and without, this return 100.130531,T but me if the number is 100T is return 100,3T and 10T is return 10.32T and is return 1.325T

-- By Lazxr

local suffixArray = {"K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Do"} -- Suffixes actuels pour les grandes valeurs

local Abbreviation = {}

function Abbreviation:getFirstDigits(value: number, digits :number)
	assert(type(value) == "number", "The value type isn't a number")

	local valueString = tostring(math.floor(value))
	local firstDigits = valueString:sub(1, digits)

	return firstDigits
end

function Abbreviation:getNumberLength(value: number)
	assert(type(value) == "number", "The value type isn't a number")

	local valueString = tostring(value)

	return valueString:len()
end

function Abbreviation:getAbbreviation(value: number)
	assert(type(value) == "number", "The value type isn't a number")

	local valueLength = self:getNumberLength(value)
	local abbreviatedResult

	for i, suffix in ipairs(suffixArray) do
		local exponent = 10 ^ (3 * i)
		local exponentLenght = self:getNumberLength(exponent)
		local nextExponentLenght = self:getNumberLength(10 ^ (3 * (i + 1)))
		local nextFloatedNumberOfValue = (10 ^ (valueLength - exponentLenght))

		if valueLength >= nextExponentLenght then continue end

		if valueLength >= exponentLenght then
			local abbreviatedValue = (value / exponent * 10) / 10

			abbreviatedResult = tostring(abbreviatedValue) .. ",".. suffix
			
			break
		end
	end

	return abbreviatedResult
end

return Abbreviation

and i test this

function Abbreviation:getAbbreviation(value: number)
	assert(type(value) == "number", "The value type isn't a number")

	local valueLength = self:getNumberLength(value)
	local abbreviatedResult

	for i, suffix in ipairs(suffixArray) do
		local exponent = 10 ^ (3 * i)
		local exponentLenght = self:getNumberLength(exponent)
		local nextExponentLenght = self:getNumberLength(10 ^ (3 * (i + 1)))
		local nextFloatedNumberOfValue = (10 ^ (valueLength - exponentLenght))

		if valueLength >= nextExponentLenght then continue end

		if valueLength >= exponentLenght then
			local abbreviatedValue = (value / exponent * 10) / 10
			local notFloatedValue, floatedValue = math.modf(abbreviatedValue)
			local getDigit = self:getFirstDigits(floatedValue * (10 ^ nextFloatedNumberOfValue), nextFloatedNumberOfValue)
			
			print(getDigit)

			abbreviatedResult = tostring(abbreviatedValue) .. suffix
			
			break
		end
	end

	return abbreviatedResult
end

but the floatedNumber return is 1.3053100000000485e+99

There is actually a much simpler way of handling this using log base 1000. Basically for every 1000x the value, log base 1000 goes up by one, effective following the pattern of suffixes if you floor the result. Here’s an example I made using this method, as well as having a specified digits count to display:

local suffixArray = {"K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "De"}
-- number of digits to display
local digits = 4

function getAbbreviation(value: number)
	local index = math.floor(math.log(value,1000))
	local suffix = index > 0 and suffixArray[index] or ""
	-- convert value to first digits
	local displayedNum = value/(1000^index)
	-- calculate digits to add to round
	local displayDigitsAdd = digits - math.ceil(math.log10(displayedNum))
	local displayFloorMult = 10^displayDigitsAdd
	-- rounds to desired digits
	local finalDisplayedNum = math.floor(displayedNum*displayFloorMult)/displayFloorMult
	return tostring(finalDisplayedNum) .. suffix
end

print(getAbbreviation(1234567.89)) -- 1.234M
print(getAbbreviation(12345678.9)) -- 12.34M
print(getAbbreviation(123456789)) -- 123.4M

Let me know if you had any questions. Hope this helps!

oh okayyy thanks you, i’m a novice x)

I’ve never really been interested in math, more in logic and optimization, but I’ve noticed that math is actually an important part of optimization. I don’t know where you learned to handle all these formulas probably experience but one day, I know I’ll master math!

I made a simple function for this a while back:

It’s very similar in logic with @BuilderDolphin’s response.

1 Like

I’m sure you’ll be able to improve a lot over time as you learn more. A lot of the maths I learned for coding are from school (trigonometry especially), but you can always learn ahead of time if you like. Lots of practice also helps!

1 Like