Rounding numbers to a whole

I made a script that displays the orientation of a part inside of a car. it works but it shows the EXACT number. I want to round that number to the nearest whole number. I have tried math.round() but it doesn’t work.

local car = script.Parent.Parent.Parent.Parent.Car.Value
while true do
	wait(0.5)
	script.Parent.Text = (car.Body.ROLL.Orientation.X)
end

Please look for the answer in pre-existing posts before making a new one :slightly_smiling_face:

I have, but I could not find any solutions. :frowning:

The first thing that popped up when I googled “roblox round to nearest whole number” was this. for the actual code, use

local function round(n)
	return math.floor(n + 0.5)
end

what do you mean it doesn’t work, what did you try and what was the result

print(math.round(0.999)) -- prints 1
print(math.round(0.4)) -- prints 0
print(math.round(5.5)) -- prints 6

note: you have 3 common options

math.floor(num) -- rounds down
math.ceil(num) --rounds up
math.round(num) --rounds to the nearest

and you can use @XxFishShadowxX method, but I would just suggest use round

2 Likes

image
when i use math.round() i get this error message.

what is the exact code, copy and paste the line with math.round.

local car = script.Parent.Parent.Parent.Parent.Car.Value
while true do
	wait(0.5)
	script.Parent.Text = (car.Body.ROLL.Orientation.X)
end

where is math.round, I meant the code that caused the error, but here, what you want is this

script.Parent.Text = math.round(car.Body.ROLL.Orientation.X)

script.Parent.Text = math.round(car.Body.ROLL.Orientation.X.math.round())

now that I know what you typed, this makes it easier, thank you, yes just remove the math.round at the end there, you only need the first one

1 Like

script.Parent.Text = math.round(car.Body.ROLL.Orientation.X)

1 Like