Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Typecasting question?

Rook Inventor
Registered User
Join date: 11 Mar 2009
Posts: 22
03-17-2009 17:27
Can you double typecast something? Like I need a string that is an integer of a float? In other words, say I have float APPLES that has a value of 10.00000. I want to express to the owner he has 10 apples. Not 10.00000 apples. In order to say it, i have to convert APPLES to a string, but I need to convert it to an integer also.

I realize I can just create another variable like integer APPLES1 = APPLES, but isn't that a waste of memory?

I thought llRound would be the key, but it seems it just rounds it, but doesn't change the variable to an integer. In other words I tried this

Float APPLES = 10.0;
APPLES = llRound(APPLES);
llSay(0,(string)APPLES);

and it still says 10.00000
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
03-17-2009 19:32
APPLES is typecast as a float and can not be recast, nor would you want to. That would be a pretty bad coding no no.

Try it like this:

float APPLES = 10.00;
llOwnerSay((string)llRound(APPLES));

which returns 10

or to use it elsewhere in the script you can use it like so:

integer ORANGES = llRound(APPLES);
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Rook Inventor
Registered User
Join date: 11 Mar 2009
Posts: 22
03-17-2009 20:07
Yes that worked! Thank you!
Ron Khondji
Entirely unlike.
Join date: 6 Jan 2007
Posts: 224
03-18-2009 01:04
llSay(0,(string)((integer)APPLES));

also does the trick.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
03-18-2009 09:17
you do want to be careful and not report a value that has not been fixed.

eg
float apples = 2.7
llSay( 0, (string)((integer)apples)) );

will say "2", even though the value is stored as '2.7'

same goes for
llSay( 0, (string)llRound( apples ) );

the internal value isn't rounded, so be careful if the reported difference might change the outcome of later calculations, or the user may not be able to gauge accurate outcomes.
_____________________
|
| . "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...
| -
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
03-18-2009 10:05
From: Void Singer
you do want to be careful and not report a value that has not been fixed.

eg
float apples = 2.7
llSay( 0, (string()((integer)apples)) );

will say "2", even though the value is stored as '2.7'

same goes for
llSay( 0, (string)llRound( apples ) );

the internal value isn't rounded, so be careful if the reported difference might change the outcome of later calculations, or the user may not be able to gauge accurate outcomes.


Don't forget llFloor() and llCeil which round towards negative infinity and positive infinity respectively... To 'round out' the rounding functions (boo!)
_____________________
My tutes
http://www.youtube.com/johanlaurasia
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
03-18-2009 20:22
I was just covering the stated examples, but yeah... it really applies to ANY change that isn't saved back to the variable if that variable is used later in the script.

it applies to all variable types... and all functions that can be used modify their displayed value, without actually instituting a change.
_____________________
|
| . "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...
| -
Lear Cale
wordy bugger
Join date: 22 Aug 2007
Posts: 3,569
03-19-2009 08:00
Keep in mind that double-casting fails for key -> string -> list (or is it string -> key -> list).

For example, these are legal (and equivalent):

From: someone

mylist += (list)((string)id);
mylist += [(string)id];
But they don't work correctly. Or it might have been these that fail:

From: someone

mylist += (list)((key)str);
mylist += [(key)str];
Or maybe both. As usual, Argent knows the details. In any case, when adding items to lists that are keys but need to be stored as strings, or vice versa, I always assign them to a temporary variable first, e.g.:

From: someone

string str = (string)id;
mylist += (list) str;
In case you're not familiar with ";(list) foo", it's essentially the same as "[foo]", but does not allow multiple items like "[foo, bar]" does. In original LSL, it took less memory; with Mono there seems to be no difference.
Rook Inventor
Registered User
Join date: 11 Mar 2009
Posts: 22
03-19-2009 17:57
Void,

I am working with purely integers at the moment (dice) so not decimal is needed, but one of the functions I was using required a float.

Thanks for all your help everyone :)