Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Typecast Float to String

FoxyHollow Guardian
Registered User
Join date: 29 Oct 2009
Posts: 9
12-03-2009 14:49
If an float produces an number like for example 7.850000, is there an good way to typecast it to an string and keep all the numbers after the coma? For example 7.85 or in worst case 7.850000?

It's not a very big deal if I keep all the zeroes, i'll just delete them with llDeleteSubString (I think it is), but I'd need to keep the .85 part of the float.
Dora Gustafson
Registered User
Join date: 13 Mar 2007
Posts: 779
12-03-2009 15:22
did you try to typecast it?

float x=7.85;
string s=(string)x;

that should do it
_____________________
From Studio Dora
Innula Zenovka
Registered User
Join date: 20 Jun 2007
Posts: 1,825
12-03-2009 15:32
http://wiki.secondlife.com/wiki/Float2String might be what you need.
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
12-03-2009 16:39
if you are just looking to chop trailing zeros off, it's actually the more difficult problem, which can be tackled 2 ways....

method one: loop through the back end until you hit something besides zero, then remove everything after it.

CODE

string uTrimFlt2Str( float vFltSrc ){
if (vFltSrc == (integer)vFltSrc){
return (string)((integer)vFltSrc);
}
else{
string vStrSrc = (string)vFltSrc;
integer vIntCnt = llStringLength( vStrSrc );
while ("0" == llGetSubString( vStrSrc, --vIntCnt, vIntCnt )){
}
return llGetSubString( vStrSrc, 0, vIntCnt );
}
}


method two, convert zeros to spaces, and trim them off the end.
CODE

string uTrimFlt2Str( float vFltSrc ){
if (vFltSrc == (integer)vFltSrc){
return (string)((integer)vFltSrc);
}
else{
return llDumpList2String(
llParseString2List(
llStringTrim(
llDumpList2String(
llParseStringKeepNulls(
(string)vFltSrc, ["0"], [] ),
" " ),
STRING_TRIM_TAIL ),
[" "], [] ),
"0" );
}
}


trailing decimals are removed by integer comparison in the first test (if they exist) otherwise all zeros are truncated
_____________________
|
| . "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...
| -