Fixed Precision
De DigiWiki.
If you need to cast a float to a string, you'll get lots of extra zeros you probably don't need. This function will let you specify a fixed precision for the resulting float->string conversion. Both functions are based on the assumption that LSL will use the default precision for the float->string conversion, which is to say 6 decimal places.
Fast and accurate
string fixedPrecision(float input, integer precision) { precision = precision - 7 - (precision < 1); if(precision < 0) return llGetSubString((string)input, 0, precision); return (string)input; }
Faster, and uses 7 fewer bytes then the above (and just as accurate).
string fixedPrecision(float input, integer precision) { if((precision = (precision - 7 - (precision < 1))) & 0x80000000) return llGetSubString((string)input, 0, precision); return (string)input; }
The second version of the funcion exploits some quirks in LSL; with the result of faster tighter code. LSL has always displayed 6 decimal places and I have never seen it do otherwise, even when the number is at the extreams of the range.


