How do I remove decimals

%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.