Converting a Float to an Integer
TurnipTruck
Posts: 1,485
Greetings,
I need to convert a float variable to an integer. Whether the float value rounds up or down to the integer is of no consequence.
Any ideas? Thanks.
I need to convert a float variable to an integer. Whether the float value rounds up or down to the integer is of no consequence.
Any ideas? Thanks.
0
Comments
DEFINE_VARIABLE
FLOAT fFloat
INTEGER nInteger
And then just type_cast when you need to like this:
nInteger = TYPE_CAST(fFloat)
So if you wanted 2.4 x 10.8
Then
2.4 x 10 = 24
10.8 x 10 = 108
Then
24 x 108 = 2592
2592 / 10 = 259.2
Now to convert the float result to an integer type_cast the result.
This is all fuzzy to me but it has something to do with the way Netlinx handles floating decimal pointers. Apparently it doesn't handle them well.
If you search for floats or floating you should fine numerous posts about this.
Except that 2.4 x 10.8 is 25.92... maybe you should let the processor handle the decimal places instead. I'm not aware of any issues with floating point numbers that would be solved by using fixed point math.
format is an option as well.
atoi(format('%3.0f',fFloat)) might do the trick although I haven't used in this way so ymmv.
Paul