Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

cant find it anymore :( float2integer

TheLoneWolf Arkin
Registered User
Join date: 26 Jan 2007
Posts: 29
12-21-2007 09:47
i know its out there somewhere but i cant find it no more :(

how to convert a float to an integer?

i know there was somewhere a page that explained them all but i believe it was on the old wiki :(

tnx
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
12-21-2007 09:57
Just cast it...

float f = 1.2;
integer i = (integer)f;
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
12-21-2007 10:26
That method loses information. I think what he is referring to is Strife's float<->integer casting functions, which pretty much take the bits in the float and put the same bits in the same places in an integer. Basically reading the float AS an integer, rather than converting (and truncating) TO an integer.

The code I think you are looking for is on his Wiki user page:
http://wiki.secondlife.com/wiki/User:Strife_Onizuka
Janice Betsen
Registered User
Join date: 29 Aug 2007
Posts: 95
12-21-2007 11:07
If close is all you are after ...

Take Sindy's example and add a little bit of rounding.
IIRC, casting is just like truncating or simply dropping the fractional portion.
So (integer)1.2 == (integer)1.9
This is easy to fix.

integer Float2Int (float f){
return ((integer)(f+0.5);
}
.....
integer nSomeVar = Float2Int(fSomeFloat);

Most of the time, I only need to go from float to Int for user output, in which case his works fine.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
12-21-2007 16:39
o.0

float ourFloat = 1.5;

if you want to ignore the fractional part
(integer)ourFloat

if you want to round to the nearest integer
llRound( ourFloat )

if you want to always round down (same as ignoring it if it's positive)
llFloor( ourFloat )

if you want to always round up
llCeil( ourFloat )
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
TheLoneWolf Arkin
Registered User
Join date: 26 Jan 2007
Posts: 29
12-21-2007 20:20
tnx ppl now i know where to find it again :p