Don't click here unless you want to be banned.

LSL Wiki : ExampleFixedPrecision

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl338.us.archive.org
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.

Examples
There is no comment on this page. [Display comments/form]