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