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

How would I round down a decimal with code?

Basically how do I turn 9.12761656 to 9.12? I tried using math.floor but it turns it into an integer. Thanks!

2 answers

✓ Accepted answer

One way would be to multiply it by 100, floor that, then divide it by 100.

9.12761656 * 100 = 912.761656
floor of 912.761656 = 912
912 / 100 = 9.12

Hope this helped

1 comment

0

I've created a function for this, so you are right too, but I have a feeling mine is easier to use.

I've seen this question before. Here's a quick problem-solver. I've created a function that rounds a long decimal into a short decimal:

local function decimalRound(num,round)
	-- Checking if both variables are valid numbers, otherwise throws an error.
	assert(tonumber(num),"Variable num is not a number.")
	assert(tonumber(round),"Variable round is not a number.")
	
	-- Calculations
	local result = num
	local roundNum = 10^round
	result *= roundNum
	
	result = math.floor(result)
	result /= roundNum
	
	-- Return the "result" variable
	return result
end

Example of using decimalRound():

local x = decimalRound(9.12761656,2)
print(x)

The following example would return:

9.12

No problem, from Canada. :)

2 comments

0

what does assert mean??

0

^ whenever the first parameter of assert is false or nil, it errors the second parameter

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