SOLVED Converting a value to int?

Neptune

Member
If I have a float, say 17.6, and I want to floor to an integer (17) I normally just divide by 1.
GML:
var my_int = 17.6 div 1; //contains integer 17
Is there a function or something for this - converting to an integer? Sometimes using DIV feels sketchy...
 

chamaeleon

Member
I guess you could use floor().. You might want to consider what you want the result to be if the value is negative though, as -17.6 would be floored to -18. And also, do you just want the value 17, or do you want an actual integer (int64)?
 

chamaeleon

Member
Side-note, the div function will give the result 17, but the data type is still a real (confirmed with is_real() and is_int64(), giving a 1 and 0 result, respectively).
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
I think I want an actual integer. I'll try the int64, thank you!
GML doesn't really have "proper" typing and most values you use in variables will be floats, regardless of what you want them to be. Some built in variables are ints or other types, but that's pretty much internal and you should just treat all values in GM as floats unless otherwise specified by the documentation. I suspect we'll be getting explicit typing for variables in the future though, as there are so many "exceptions" to this rule now - and we have dedicated type functions for checking things - that I think it would only be a question of time before we can declare ints, floats, vecs, etc... as variables.
 
Top