Rounding Abbreviation

Hello, I have this function that abbreviates numbers and it’s working well apart from when it has a whole number. Instead of displaying the whole number it would displaying something like 10.00, Is there anyway I could modify this so it would just display 10? I thought about using math.round() but this wouldn’t work very well in my case due to the output of the abbreviation function being a string.

local function Format(value, idp)
	local ex = math.floor(math.log(math.max(1, math.abs(value)), 1000))
	local abbrevs = abbrev [1 + ex] or ("e+"..ex)
	local normal = math.floor(value * ((10 ^ idp) / (1000 ^ ex))) / (10 ^ idp)

	return ("%."..idp.."f%s"):format(normal, abbrevs)
end

Example:
image

3 Likes

did you use tonumber() to convert your string to a number?

3 Likes

That doesn’t really work for any of the numbers with a letter in them such as K or M.

2 Likes

Use math.floor()

It removes the decimal part of a number and returns the largest integer less than or equal to the given number.

local num = 15.75
local result = math.floor(num)

print(result)  -- Output: 15

4 Likes

You can use string.match to get all the numbers from the text and then convert it to number.

local text = "10.00K" 
local numberFromText = string.match(text,"%d+") -- get only the numbers from the text
local convertToNumber = tonumber(numberFromText)  -- convert it to a number
print(convertToNumber ) -- Output : 10
1 Like

I’m suspecting this is your problem, the string.format is telling it to use %.2f which means 2 decimal floating point number as in $3.00 $45.00 etc…

Or, it may be karma telling you that it is cruel to create an item with a 1/10,000 chance of winning it! :slight_smile:

3 Likes

Module Script

local suffixes = {
	"",
	"K",
	"M",
	"B",
	"T",
	"Qa",
	"Qi",
	"Sx",
	"Sp",
	"Oc",
	"No",
	"Dc",
	"Ud",
	"Dd"
}

local DP = {}


function DP.Abbr(number)
	local abbreviationFactor = math.floor(math.floor(math.log10(number)) / 3)
	local abbreviation = ""
	if abbreviationFactor > 0 then
		abbreviation = string.format("%.2f", number / 10 ^ (abbreviationFactor * 3)) .. suffixes[abbreviationFactor + 1]
	else
		abbreviation = tostring(number)
	end

	return abbreviation
end

return DP

Usage

local DigitProfiler = require(game.ReplicatedStorage:FindFirstChild("Modules"):FindFirstChild("Digit Profiler"))
Text = DigitProfiler.Abbr(Chips.Value)

This fixes your problem and also allows you to add custom abbreviations if needed. If you want to fix your original script than just add a math.floor to the value.

@Sir_Highness I advocate it :stuck_out_tongue_winking_eye:

2 Likes

Judging by the screenshot, this is a rarity collector, meaning that with progression that can very quickly become 1/1… At least do research before being passive aggressive :sob:

3 Likes

You see the smiley emoji? It was a joke. You know what a joke is?

1 Like

I’m acoustic. It’s hard for me to see what the tone of a message is, so I interpret it from the language. The smiley is very often used in conjunction with passive aggression.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.