Why won't this round to the nearest hundredth?

	--edits the time of the int values so that the bullets know the time.
	fullTime.Value = math.floor(((song.TimeLength*100)+0.5))/100
	currTime.Value = math.floor(((song.TimePosition*100)+0.5))/100

This script is supposed to get the time value of a song and floor it to the hundredth, but it’s not working? What would be the issue?

1 Like

Ok, let’s say TimeLength is 50. So for the process, it would be:

  1. 50 * 100 --> 5000
  2. 5000 + 0.5 --> 5000.5
  3. math.floor(5000.5) --> 5000
  4. 5000/100 --> 50

So, uhhh, the output number is the same as the input number…???

Here, let me give you an example. The number I would usually get for time is something like this “1.4543793”. I would like to just get the first two decimal numbers.

What is the output you are getting? Is it just the same as the input?
Try splitting it up and see where the problem is.
such as
local temp = song.TimePosition
print temp
temp = temp * 100
print temp
temp = temp + .5
print temp
temp = math.floor(temp)
print temp
temp = temp / 100
print temp

So you can check each step of the process

If you are trying to get just the two decimal numbers, can’t you just::

  1. Convert the number into a string
  2. Use string match to get the two decimals by using string patterns.
  3. Convert the result to a number

I could provide a short code of it if you want.

local song = script.Song
song.Loaded:Wait()  -- Wait for the song to finish loading

local length = song.TimeLength
local roundedLength = math.floor(length * 100 + 0.5) / 100

print("Rounded Length:", roundedLength)

Here is one I found after a search.

module.RoundDec = function(value,precision,up)
	precision = precision or 0
	value = tonumber(value) or 0
	value = value * math.pow(10,precision)
	if up then
		value = math.ceil(value)
		value = math.floor(value)
	value = value / math.pow(10,precision)
	return tonumber(string.format("%."..precision.."f",value))

I always get a whole number, not a number that is #.## form.

Let me clarify, let’s say I have a number 3.67828. You only want to get the first 2 decimal numbers, which is .67, without rounding it up or down. Is this correct?

If you have that number I would want 3.67.

If the number was 16.35327, I would want 16.35.

If it was 134.321443, I would want 134.32. I just want the first two decimals and the whole number.

This worked at some point . . . not sure what I did. Maybe it is because it’s an int value object. Do they take decimals?

Integers do not accept decimals.

But anyways, use the script provided by @SelDraken with the precision as 2 and up as false.

That is why! I was not using a number value (instead I was using an int value). Thanks for the help, my rounding program actually worked.

1 Like

Also, look at math.round instead of having to recalculate math.floor or cath.ceil.
It does exactly what you want, and you only have to do this:

value = math.round(value * 100)/100