Game Development Community

Float to Integer Conversion?

by Dustin Sims · in Torque Game Builder · 12/17/2006 (10:07 pm) · 8 replies

Hello All,

I am having some trouble comparing values that I receive from GetPositionX();
It of course sends back Floats that are usually something like 275.0001 or other.
I want to use that number in an equation to get a nice whole "integer". Is there a function
that converts floats to integers. If not, what is the work around?
I would think this comes up alot, What does everyone else do???
Thanks in advance...

#1
12/18/2006 (12:41 am)
TGB using variant so variable can contain string , float or integer , why you not try using round , ceil or floor
#2
12/18/2006 (2:08 am)
MAbs() should work too if I'm not mistaken...
#3
12/18/2006 (6:07 am)
Thanks Michael S.
My Only problem with using floor and ceil is that I would still have to perform a check on every single number to determine if I wanted to round up ro down. I have a LOT of numbers that I want to round. I do know what I want them to end up being but still would hate to have to check each one.
Ex. I would have to code, If # is 274.9999 then round up, If # is 275.00023 then round down.
I figured there should be a round_to_nearest whole number function...

... I think I just found it...

mFloatLength(%float, %numdecimals);
Purpose
Return a string containing the float formatted with the specified number of decimal places.
Hopefully it will work with 0 as the %numdecimals....
I'll check it and get back with..........

Drew-Gaiiden-Sikora,
Mabs() only changes negative numbers to positive if I'm not mistaken. It still returns a float.
#4
12/18/2006 (6:30 am)
Strange decision to use mFloatLength(%float, %numdecimals);, isn't it?
Try to use on 275.999, and what would you get?
You should try to use floor or ceil function as mentioned above. Or use non strict equation in you
if statements.
#5
12/18/2006 (6:48 am)
%rounded_number = mFloor (%floatNumber + 0.5); should do what you want.

Note that this only works properly for positive numbers though... if you need it to work with both positive and negative ones it's probably best to write a function, something like this;

function roundNumber (%number)
{
%val = 0;
if (%number < 0)
%val = mFloor (%number - 0.5);
else if (%number > 0)
%val = mFloor (%number + 0.5);

return %val;
}

(this is completely untested, but it should work... hopefully).
#6
12/18/2006 (3:48 pm)
Thanks for the help everyone..

Magnus,
The function you suggested seems like it will work for my purposes.
I haven't tried it yet, but as you said, It Should work.
Again, thanks for all the responses.
#7
12/27/2006 (3:40 pm)
I just wanted to give everyone who looked at this post an update.
I was using the Roundnumber function that Magnus had suggested and it does work great.
I was just cleaning up my script files and tinkering with the
mFloatLength(%float, %numdecimals);
function again and it does work as I had thought.
It will properly round a number up or down to the nearest integer if given 0 as the %numdecimals.
I went ahead and used it instead of the scripted Roundnumber solution.
I imagine it should be a tad faster than the script and it helps keep my scripts clean...
Again, thanks for the help everyone..
#8
12/29/2006 (6:00 am)
I use this function. %val being the number to round. and %num being the number to round to.

function roundnumber(%val, %num)
{
%rem = %val % %num;
if(%rem>%num/2)
{
%val = %val - %rem + %num;
}
else
{
%val = %val - %rem;
}
return %val
}