Rounding numbers by 0.5 and 0.1

I can’t seem to round numbers to the nearest 0.5. What I’m trying to do is to make 1.5000002831 to just 1.5. I’ve read some other posts but I couldn’t find a solution from those posts. This is the script that I am using right now:

local vol = math.floor(sound.Volume) -- Volume is set to 1.5
  print("Volume".." "..vol)

and it prints out

Volume 1

Please help, and thanks :slight_smile:

1 Like

The reason the number is some odd value just above 1.5 is because of how numbers are stored in a PC. Simply put, bits are great for storing medium sized numbers from 10.0 to 999.99 (the numbers are much more different, this is just an analogy), but there is a threshold on both sides whether big or small where the number is no longer super accurate.

This is simply an issue in computer architecture. Though, the extra .0000000281 is not going to affect things at even a minor level.

Edit: at least that’s how I see this
Edit 2: You also changed your post from what it was.

Sorry, I don’t understand what you said. the reason i want to round is because i want the number to be displayed on a TextLabel, and yes i did change my post because it was abit messy

This is similar to a griding function.
Something like:

math.floor(value*10)/10

Is an easy way to round a number to the nearest tenth of a number. Flooring with the math function returns the nearest whole number, so this is just a way to move it to the tenths position.

And pardon me, I didnt totally understand the question.

10 Likes

Not sure if this could work?

local Volume = sound.Volume -- sound.Volume == 1.5000002831
RoundedVolume = math.floor(Volume * 10) -- Volume * 10 would be 1.5000002831 * 10, so 15.000002831, math.floor would make it 15
DividedVolume = RoundedVolume / 10 -- Divide the RoundedVolume back by 10 to get 1.5
sound.Volume = DividedVolume -- 1.5

Thanks! it worked perfectly :smiley:

1 Like

It worked, but it wasnt what i was looking for, thanks though! :smiley:

I mean it’s the exact same as @GreekForge just explained a bit more in detail :man_shrugging:

Sorry if i was unclear in my post, this is my first

Yes it is, but if i could have 2 solutions i would put you and @GreekForge 's posts as the solutions
What I mean is that both you and Greek had the same solution i needed, so thanks