How would I round any digit to a 2 decimal position?

  1. What do you want to achieve? I want to make a timer that I can round to a 2 decimal position (for example, 3.63468 would go to 3.63)

  2. What is the issue? The issue is I am unable to find ways to do so

This is the script I am currently using:

local UIS = game:GetService("UserInputService")

local OnOff = false

UIS.InputBegan:Connect(function(i,t)
	if t then return end
	if OnOff == false then
		if i.KeyCode == Enum.KeyCode.Space then
			OnOff = true
			local timer = 0
			repeat
				wait(.01)
				timer += .01
				script.Parent.Text = timer
			until OnOff == false
			
		end
	else
		if i.KeyCode == Enum.KeyCode.Space then
			OnOff = false
		end
	end
end)

multiply by 100
math.floor(number+.5)
divide by 100

4 Likes

Why does this work?? Oh well, atleast there is a solution.

it works because:
multiply by 100 makes it 100 times larger, or, the hundredth decimal place now becomes the ones digit (like 0.01 would become 1)

floor(number+.5): if a number’s thousandth decimal place is above or equal to 5, the number’s ones digit will go up when .5 is added to it, otherwise, it will still have the same ones place. Now, you floor it, which gets rid of all decimal digits (so like, 0.123 through the 2 steps would become 12.3+.5, or 12.8. 12.8 floored is 12, meanwhile, if its was 12.5+.5, it would become 13, which when floored, stays at 13)

divide by 100: you timed it by 100, so you obviously have to divide it back down so the ones digit is again the hundreth digit

btw boel has a better explanation i think

3 Likes

here are all the math functions.

math.floor (number) removes everything behind the decimal point.
but you want to round it up if the value behind the dot is bigger than 0.5

lets say you have 16.3
16.3+ 0.5 = 16.8
remove all numbers behind the decimal and you get 16

if your number is 16.6
16.6+0.5=17.1
remove all numbers behind the decimal and you get 17

it is to put you just over the whole number

you can make your own function if you need to do it repeatedly

function round (number, decimals)
number = number*10^decimals
number = math.floor(number+0.5)
number = number / 10^decimals
return number

-- wrote this on the fly you might need to spellcheck it
1 Like

Ok, I understand now. I was looking at math.floor and wondering if i could manipulate it to work and I guess I could.

1 Like

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