How would I round to the nearest 50 via script?

Well, the title kinda says it all. I want to know if there’s a way if I can round to the nearest 50, and if so, how I would do it. For example, a player has 2347 XP, so it would automatically round to 2350, or the player has 4318, so it would round to 4300.

Result = Number - Number % 50

Would be how you do it, although this only rounds it down

That will only round down. To round something to the nearest x you would have to make use of math.round:

local function roundNumberToNearest(x, nearest)
    return math.round(x / nearest) * nearest
end

Basically how this function works is it takes any number, converts it to a “unit” (or percent of nearest) form, then rounds it, and finally multiplies back up.

Think of it this way:

  • We have a number, 95
  • We want to round it to the nearest 50
  • 95 is 190% of 50
  • We want the percent rounded to the nearest 100%
  • 190% is nearest to 200%
  • 200% of 50 is 100
3 Likes