Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can someone help me with abbreviating decimals?

I need help with this problem. I am making a simulator with an upgrade shop that upgrades by 1.25x, but the number gets so long because of the decimals, for example, 9.31322574615x Cash. I want to abbreviate it to having only 2 decimal digits instead of so many, but I don’t know how to do it. I know how to abbreviate stuff to the thousands or millions or billions but not decimals. I can’t seem to find this problem anywhere on the dev forum. I even tried searching it on YouTube, but it just gave me info on how to do it in languages like Java or Python or C++ and not Lua. Please help me if you can and I would really appreciate it!

2 answers

I went ahead and wrote you a function which rounds your number to the given decimal point.


local number = math.pi	--example number

local function round(num, decimals)	-- 'num' is the number you want to round, 'decimals' is the decimal point you want to round to
	
	local const = 10 ^ decimals
	local num = num * const	--Shift the decimal point to simplify rounding
	
	local compare = num - math.floor(num)	--isolate the decimals we want to round
	
	if compare < 0.5 then	--The logical operators are just rounding up or down to the closest whole number
		
		num = math.floor(num)	--round down
	
	else
		
		num = math.ceil(num)	--round up
		
	end
	
	return num / const	--return the decimal point to its original position
	
end

print(number)	-- Outputs 3.1415926535898
print(round(number, 2))	--Outputs 3.14

All you need to do is round the number. This is the best and most simple way to round numbers in roblox:

Number = 9.31322574615 -- Whatever the number is

RoundedNumber = math.floor(Number * 100 + 0.5)/100 -- if you want it rounded to the tenths place make it *10 and /10, if you want it rounded to the hundredths place make it *100 and /100 and so on

print(RoundedNumber)

I hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.