Help with updating that function work with negative numbers

  1. What do you want to achieve? Keep it simple and clear!
    Topic says all about what i need
  2. What is the issue? Include screenshots / videos if possible!
    I have no idea about it
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked few topics, though nothing helps

Function:

local function Suffix(Number, Precision)
	local Suffixes = {
		"", " K"," M", " B", " T",
		" Qd", " Qn", " Sx", " Sp",
		" Oc", " No", " De", " Un",
		" Tr", " Qud", " Qun", " Sxd",
		" Spd", " Ocd", " Nod", " Vg",
		" UVg"," DVg"," TVg"," QdVg",
		" QnVg"," SxVg"," SpVg"," OcVg",
		" NoVg"," Tg"," UTg"," DTg"," TTg",
		" QdTg"," QnTg"," SxTg"," SpTg"," OcTg",
		" NoTg"," qg"," Uqg"," Dqg"," Tqg"," Qdqg",
		" Qnqg"," Sxqg"," Spqg"," Ocqg"," Noqg",
		" Qg"," UQg"," DQg"," TQg"," QdQg"," QnQg",
		" SxQg"," SpQg"," OcQg"," NoQg"," sg"," Usg",
		" Dsg"," Tsg"," Qdsg"," Qnsg"," Sxsg"," Spsg",
		" Ocsg","Nosg"," Sg"," USg"," DSg"," TSg",
		" QdSg"," QnSg"," SxSg"," SpSg"," OcSg"," NoSg",
		" Og"," UOg"," DOg"," TOg"," QdOg"," QnOg"," SxOg",
		" SpOg"," OcOg"," NoOg"," Ng"," UNg"," DNg"," TNg",
		" QdNg"," QnNg"," SxNg"," SpNg"," OcNg"," NoNg"
	}

	local Index = math.floor(math.log10(Number) / 3)
	if Index < 0 then
		Index = 0
	end
	local Suffix = Suffixes[Index + 1]

	Number = Number / (10 ^ (3 * Index))
	Number = tostring(math.floor(Number * 10 ^ Precision) / 10 ^ Precision)

	return Number .. Suffix
end

It currently gives an error while working with negative numbers one

Number cannot be less than or equal to 0, because this value is either undefined or imaginary in mathematics; log10(0) = undefined and log10(-1) = imaginary number.

I am unsure as to what the purpose of your code is, but if you simply want to get rid of the error, you can either set a minimum value for Number or you can disregard the code whenever Number is ≤ 0.

Example of the former:

if Number <= 0 then 
	Number = 0.01 
end

Example of the latter:

if Number <= 0 then 
	break --replace this with a return instead if needed 
end

Well purpose of code is something like loan, player balance just go negative, like - 20, but this function add suffixes, so like if player balance is 1000 it will say 1K, and i want to do same with negative ones

is Number here a variable for loan ?

you can just treat the negatives as positives, here’s the fixed code (with some better practices…)

local Suffixes = {
    "", " K"," M", " B", " T",
    " Qd", " Qn", " Sx", " Sp",
    " Oc", " No", " De", " Un",
    " Tr", " Qud", " Qun", " Sxd",
    " Spd", " Ocd", " Nod", " Vg",
    " UVg"," DVg"," TVg"," QdVg",
    " QnVg"," SxVg"," SpVg"," OcVg",
    " NoVg"," Tg"," UTg"," DTg"," TTg",
    " QdTg"," QnTg"," SxTg"," SpTg"," OcTg",
    " NoTg"," qg"," Uqg"," Dqg"," Tqg"," Qdqg",
    " Qnqg"," Sxqg"," Spqg"," Ocqg"," Noqg",
    " Qg"," UQg"," DQg"," TQg"," QdQg"," QnQg",
    " SxQg"," SpQg"," OcQg"," NoQg"," sg"," Usg",
    " Dsg"," Tsg"," Qdsg"," Qnsg"," Sxsg"," Spsg",
    " Ocsg","Nosg"," Sg"," USg"," DSg"," TSg",
    " QdSg"," QnSg"," SxSg"," SpSg"," OcSg"," NoSg",
    " Og"," UOg"," DOg"," TOg"," QdOg"," QnOg"," SxOg",
    " SpOg"," OcOg"," NoOg"," Ng"," UNg"," DNg"," TNg",
    " QdNg"," QnNg"," SxNg"," SpNg"," OcNg"," NoNg"
}

local function addSuffix(number, precision)
    number, precision = math.abs(number), math.abs(precision or 2)

	local index = math.max(math.floor(math.log10(number) / 3), 0)
	local suffix = Suffixes[index + 1]

	number = number / (10 ^ (3 * index))
	number = (math.floor(number * 10 ^ precision) / 10 ^ precision)
	local newNumber = (`{number}{suffix}`):gsub(" ", "")
	
	return newNumber
end

You may have noticed that I removed the extra white spaces in the number, for some reason there’s a white space there and I thought it wasn’t intentional, so ya, you can remove the gsub if needed.


To insure it works with negatives, I've added `math.abs` at the top, I also did it with precision and added `or 2`, this means that you shouldn't always pass precision as an argument, the default will be 2!