Float2String
Function notes
Negative floats are fine (i.e. negative signs will be preserved.)
If you say 1 place, you will get 7.0 instead of 7.0000000
If you say 3 places, you get 7.14 instead of 7.1400000
7.1373699 will come out as 7.13 or 7.14, depending on whether you specify rounding
Example:
string myFormattedFloat = Float2String(-7.1373699, 3, TRUE);<br /> //rounded
//returns "-7.14"
string myFormattedFloat = Float2String(-7.1373699, 3, FALSE);<br /> //not rounded
//returns "-7.13"Return value notes
Returns a string that contains the float num in a tidy text format, with optional rounding.
Specification
string Float2String ( float num, integer places, integer rnd) {
//allows string output of a float in a tidy text format
//rnd (rounding) should be set to TRUE for rounding, FALSE for no rounding
if (rnd) {
float f = llPow( 10.0, places );
integer i = llRound(llFabs(num) * f);
string s = "00000" + (string)i; // number of 0s is (value of max places - 1 )
if(num < 0.0)
return "-" + (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
return (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
}
if (!places)
return (string)((integer)num );
if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000)
return llGetSubString((string)num, 0, places);
return (string)num;
}Original wiki source
Some wiki templates and tables need their original context. View this article on the Second Life Wiki. Technical wording and examples are retained from the source; historical guidance may differ from current behavior.