How to round .999999999998 numbers

i have a issue with a text label inputting numbers like this
image

local time = script.Parent.Bar.Value.Value
local waittime = script.Parent.Value.Value

if script.Parent.Visible == true then
	while wait(0.1) do
		time = time - 0.1
		script.Parent.Bar.Size = UDim2.new(time / waittime, 0, 1, 0)
		script.Parent.Bar.TextLabel.Text = string.format(time.." Seconds Left")
		if time <= 0 then
			script.Parent.Visible = false
			print("finished")
			script.Parent.Parent.Parent.Parent.ScreenGui.Finishe.Visible = true
			wait(5)
			script.Parent.Parent.Parent.Parent.ScreenGui.Finishe.Visible = false
		end
	end
end
7 Likes

I don’t know if it will change much, but since time is a global function just like print, you may want to change the variable β€œtime” to like β€œclock” or something not already defined

3 Likes

nothing is wrong with the code, its just the number its inputting

2 Likes

In that case, I found a post that might entail what you are looking for based on your title

3 Likes

the way I interpret it, you want to round it to numbers like 2.9, 2.8 correct?

3 Likes

yesβ€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž

2 Likes

Use math.round()

Extra characters

3 Likes

it will round to the nearest integer, not 0.1 decimal

1 Like

dont you need to put in every you want to round for math.round?

1 Like

Just use

math.round()

function, to round numbers.

Example:

print(math.round(1.9999)) --> 2

i want numbers to be rounded to the nearsest tenth not whole, also i have like a few hundred different numbers, and it goes through every one

Use string.format.

Code:

local timer = script.Parent.Bar.Value
local waittime = script.Parent.Value

if script.Parent.Visible then
	while task.wait(0.1) do
		timer.Value = string.format("%0.1f", timer.Value - 1)
		script.Parent.Bar.Size = UDim2.fromScale(timer.Value / waittime.Value, 1)
		script.Parent.Bar.TextLabel.Text = timer.Value .." Seconds Left"
		
		if timer.Value <= 0 then
			script.Parent.Visible = false
			print("finished")
			script.Parent.Parent.Parent.Parent.ScreenGui.Finishe.Visible = true
			
			task.wait(5)
			script.Parent.Parent.Parent.Parent.ScreenGui.Finishe.Visible = false
		end
	end
end

There are a few problems with your script that I also fixed.

  1. You should be using task.wait instead of wait
  2. You were changing the variable that stored .Value, not the actual .Value.
6 Likes

This should work for you.

number = math.floor(number * 10) / 10

This takes the number, multiplies it by 10, floors it, then divides it by 10. I had round, but 29.9999 would round to 30. Floor basically truncates the fractional part by returning the biggest integer equal to or smaller than the number you give it.

Adding onto what @Maelstorm_1973 said, you can set it into a function:

local function RoundNumber(number: number, places: number): number
   return math.round(number * (10^places)) / (10^places)
end

print(RoundNumber(1/3, 2)) --> prints "0.33"
print(RoundNumber(math.pi, 4)) --> prints "3.1416"
print(RoundNumber(1.4627365728, 1)) --> prints "1.5"

not related but how would you replace %0.1 to %1 without it just breaking, i need it for a different part of my game

	daysSinceStart = string.format("%1f", secondsElapsedSinceStart / 7200)

That’s not how you use string.format. You have to specify the amount of decimals, so it would be:

daysSinceStart = string.format("%0.0f", secondsElapsedSinceStart / 7200)
1 Like

@Katrist @Denys_Developer

Actually, you’re both wrong. It follows the printf format from C. The correct way is

local text = string.format("%5.1f", timer.Value - 1)

That gives you a total of 5 digits, with one digit to the right of the decimal. The first number is the total space needed. The second number is how much of that is past the decimal point. If that first number is not specified, then it’s whatever the integer part is.

FreeBSD Manual Pages OS 13.2-RELEASE: printf(3)

Here’s Roblox’s own documentation on the matter.

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