Need help on math.floor?

I have a value which is around 0.6986
I want to round it to 0.7

I tried using math.floor but it just gave me 1 as the output.

1 Like

math.floor(value * 10 + 0.5) / 10

2 Likes

Thanks for the fast reply. It worked.

1 Like

Instead of the equation used by @CelestialDominio you should just use
math.round(value * 10) / 10

1 Like

They both work but thank you anyway. Why should I use this instead?

Use math.round(value * 10) / 10 because its easier

It really doesn’t matter. Math round() is basically math floor(n + 0.5) so it’s just a bit simpler to use math.round().

But just to make it clear what each of these does.
Math.floor() just gets rid of everything past the decimal and keeps the original number.

Math ceil does the same thing but adds 1 in you needed to round up to the nearest whole number.

Math.round() is the rounding you learned in school and is math.floor(n + 0.5). This makes it so anything above 0.5 is rounded up and everything below is rounded down. (Floor rounds down and 0.5 + 0.5 is enough to bring it up a number before you floor it which is why this works).

And the reason your post wasn’t working is because rounding works with whole numbers and you wanted it to work with tenths of a number instead. So the *10)/10 just makes it 10 times larger for the rounding and then shrinks it back to the size you wanted after.

3 Likes

Got it, thank you very much for this detailed reply. :grinning:

1 Like

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