I have been working on a level system, and when the player level up, the xp required it multiplies x1.5, my issue is when the Max xp value is 45, and you level up, your Max xp value is 67,5 and I want to remove decimals or approximate, how I do it? I wrote this on phone, that’s why looks bad written, sorry
You can use math.floor, which rounds it down, or math.ceil which rounds it up.
-- Example
local x = 10.6
print(math.floor(x)) -- 10
print(math.ceil(x)) -- 11
You can simply either use math.floor()
or math.ceil()
to make your number either the next number up, or the next number down.
If you want to round the numbers like we were taught in school (<.5 down, >=.5 up). You can use this function:
local function RoundNumber(num)
return(math.floor(num+0.5))
end
Examples
print(RoundNumber(1.5)) --2
print(RoundNumber(1.4)) --1
print(RoundNumber(2)) --2
print(RoundNumber(2.1)) --2
If you want to properly round, use this instead:
math.floor(x + 0.5)
If you want to round up or down, @Raretendoblox or @Benified4Life’s examples are the way to go.
Lets pretend you have a value of 54.239
If you did math.floor(54.239), it’d return 54.
If you did math.ceil(54.239), it’d return 55.
Num is the value right? Like script.Parent.Value?
Yes, indeed. Make sure you feed it a number, however, as feeding it a string, even if it looks like a number, will cause it to error.
since it is with exp, I recommend using math.floor
because it rounds down always. SO, you would first detect the amount of exp you have
local Exp = reference the exp
then you put the rounding
local roundedExp = math.Floor(Exp)
You can use string formatting to turn it into a integer and then turn it back into a number.
Example:
local num = 3.14
local formatted = string.format("%d", num)
local num = tonumber(formatted)
print(num) --3
If you want the rounding then stick to the other replies
So the code I use to level up is this:
If xp.Value >= maxxp.Value then
level.Value = level.Value + 1
xp.Value = xp.Value - maxxp.Value
Maxxp.Value = maxxp.value * 1.5
So if I change maxxp.Value to rounded Exp, it will work?
you would use:
Maxxp.Value = math.floor(maxxp.value * 1.5)
next time you write code, use `` or select the code and press the button next to the left of the upload icon
Rounding down every time will actually cause net loss, meaning that over time, the player will have received less Exp than was actually awarded.
So that will remove the decinals for maxxp?
it would round it down, but you can round it up, too. You can use math.ceil
well rounding up can cause the player to gain more, which indefinitely makes them have less profit. But, this is the max xp I am talking about, not the exp. That little change does basically nothing. Sorry if this is a bit rude.
Indeed,
Using the math.floor(num+0.5)
method will actually even it out, however. You are equally likely to round up and round down. Due to regression to the law of large numbers, there shouldn’t be any net loss nor gain.
I believe that the topic has deviated enough, and I will continue this branch of the discussion no further.
Well, I would round up xp but maxxp I will round down so people can level up more easily
You should consider marking one of the replies as the solution, to indicate that no more help is needed.