How do I remove decimals

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.

1 Like

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)

2 Likes

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

6 Likes

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

3 Likes

Rounding down every time will actually cause net loss, meaning that over time, the player will have received less Exp than was actually awarded.

2 Likes

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

1 Like

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.

1 Like

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.

2 Likes

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.

1 Like

%d is for any digit though, so you never actually turn the number into an integer with this pattern. tonumber will also fail here because 3.14 does not have an integer representation - it does not implicitly drop decimals for you.

You should avoid any kind of type changing. Stick to the math library for any kind of work involving numbers. For example, use modf to separate integral and fractionals.

local num = 3.14
local integral, fractional = math.modf(num)
print(integral, fractional) -- 3 0.14
8 Likes

%d is infact for any digit, however it’ll remove any decimal places turning it into an integer.
Screenshot to prove my point:
image
You can try this yourself and it’ll always be the case.
If you really wanted to be sure then you could use the %.0f pattern.

Your use of modf is a great idea though, I may even use it myself.

1 Like

…I don’t think that’s intended? Strange, because my test didn’t format away the decimals and I don’t think it should either. It’s probably where I was running it that gave this error. If that is intended, TIL.

At the very least, if it does work, I stand by my point of not merging between data types. modf is nice for quickly whisking away the fractionals by only assigning the integral.

2 Likes

I just want to add for future readers. There are further options to explore, such as math.modf(x) where you can divide the whole number and the fraction separately. Know that what I am about to write about, is for educational usage and as previously pointed out is not adviseable to use as it is.

math.modf(x)

What happens when you input x is that it will return two values. If X has any decimals, variable2 will be returned as those decimals or else it will be 0.

Ex: “first, second = math.modf(15.1) → 15 0.1; first, second = math.modf(10) → 10 0”
Example question: How do I just return whole numbers.
While there are string manipulations that could accomplish this goal easier, this method is not bad nonetheless. You can see it as another way for a rounding function, in fact, I am going to make it like that because I have no ideas left.

local function round(n)
	local whole, decimals = math.modf(n)
	return whole --> ex: 15
    return decimals --> ex: .09
end

Also, I’d like to add that most programmers tend to avoid math functions when possible, obviously you should do the same because it puts less strain on your computer. However, I also think that readability is important and when it is something which only have an insignificant impact, I think using this approach is completely fine :slight_smile:

3 Likes

This was mentioned already.

By the way, why are you wrapping math.modf in a function call? That seems extremely pointless to do. You should not be creating an extra function just to call a function unless it’s doing more work. Leave the function out.

In addition, as I later learned, throwing away the fractional after using modf is the equivalent of math.floor. You can just floor your number. If you’re separating the integral and the fractional, you should have some kind of use case where you need to know and use both.

The purpose of the extra function was to just display the code such way where it was easier to understand. I thought titling a function would give the reader extra insight on the functionality. If you read the smaller comment above, I am already displaying the pure version of the function. The purpose of the code was for educational purposes not for usage within game development.

Sometimes people comprehend things easier with context. Of course, there may come a time where the fraction is of use, as I only used an example I could think of where I’d use this math function. I have now updated the post so it won’t further mislead other people.

is there a way to round to another one down? For Example:
10.13 to 10.1 or 10.16 to 10.2
Edit: Shouldve read more sorry for posting an unneeded reply

Multiply by 10, Round it then Divide it by 10.