Difference between revisions of "Meter2Feet"

From Second Life Wiki
Jump to navigation Jump to search
(you are correct there is a border condition with llRound I didn't see BTW your code didn't compile. MUTL is better then DIV)
(Your's didnt compile either, lol. And reciprocal value extended to guarantee for precision of result! Btw, I had checked my version by using LSLEditor, which compiled the code.)
Line 15: Line 15:
string Meter2Feet( float meter )
string Meter2Feet( float meter )
{
{
string sign = "";
    string sign = "";
if (meter < 0)
    if (meter < 0)
{ // Use only positive value to simplify the maths
    {   // Use only positive value to simplify the maths
sign = "-"
        sign = "-";
meter = -meter;
        meter = -meter;
}
    }
   
float fraction = meter * 3.2808399; // Compute feet
    float fraction = meter * 3.2808398950131233595800524934383;   // Compute feet multiplying by reciprocal value
integer feet = (integer)(fraction);
    integer feet = (integer)fraction;     // of conversion constant 0.3048 for performance reasons
   
fraction = (fraction - feet) * 12; // Compute inches
    fraction = (fraction - feet) * 12;   // Compute inches
integer inches = (integer)(fraction);
    integer inches = (integer)fraction;
   
fraction = llRound((fraction - inches) * 16); // Compute 1/16 inches
    fraction = llRound((fraction - inches) * 16);   // Compute 1/16 inches
       
string strFraction = "\"";
    string strFraction = "\"";
if (fraction != 0) // show fraction value only if not zero
    if (fraction != 0)   // show fraction value only if not zero
strFraction = " " + (string)((integer)fraction) + "/16" + strFraction;
        strFraction = " " + (string)((integer)fraction) + "/16" + strFraction;


return (sign + (string)feet + "' " + (string)inches + strFraction);
    return (sign + (string)feet + "' " + (string)inches + strFraction);
}
}
</pre>
</pre>
Line 46: Line 46:
string Meter2Feet( float meter )
string Meter2Feet( float meter )
{
{
string sign = "";
    string sign = "";
if (meter < 0)
    if (meter < 0)
{ // Use only positive value to simplify the maths
    {   // Use only positive value to simplify the maths
sign = "-"
        sign = "-";
meter = -meter;
        meter = -meter;
}
    }
   
float fraction = meter * 3.2808399; // Compute feet
    float fraction = meter * 3.2808398950131233595800524934383;   // Compute feet multiplying by reciprocal value
integer feet = (integer)(fraction);
    integer feet = (integer)fraction;     // of conversion constant 0.3048 for performance reasons
   
fraction = (fraction - feet) * 12; // Compute inches
    fraction = (fraction - feet) * 12;   // Compute inches
integer inches = (integer)(fraction);
    integer inches = (integer)fraction;
   
fraction = llRound((fraction - inches) * 16); // Compute 1/16 inches
    fraction = llRound((fraction - inches) * 16);   // Compute 1/16 inches
       
string strFraction = "\"";
    string strFraction = "\"";
if (fraction != 0) // show fraction value only if not zero
    if (fraction != 0)   // show fraction value only if not zero
strFraction = " " + (string)((integer)fraction) + "/16" + strFraction;
        strFraction = " " + (string)((integer)fraction) + "/16" + strFraction;


return (sign + (string)feet + "' " + (string)inches + strFraction);
    return (sign + (string)feet + "' " + (string)inches + strFraction);
}
}


default
default
{
{
state_entry()
    state_entry()
{
    {
llOwnerSay("Enter: [m]");
        llOwnerSay("Enter: [m]");
llListen(0, "", llGetOwner(), "");
        llListen(0, "", llGetOwner(), "");
}
    }
   
listen(integer _chan, string _str, key _id, string _msg)
    listen(integer _chan, string _str, key _id, string _msg)
{
    {
float meter = (float)_msg;
        float meter = (float)_msg;
llOwnerSay(Meter2Feet(meter));
        llOwnerSay(Meter2Feet(meter));
}
    }
}</pre>
}}</pre>
</div></div>
</div></div>
{{LSLC|Examples|Meter2Feet}}
{{LSLC|Examples|Meter2Feet}}

Revision as of 20:41, 12 September 2007

Function: string Meter2Feet(float meter);

Returns a string of metric value meter converted to feet/inch in common representation, ie. 3' 4 5/16".

• float meter distance in meters, any valid float, positive or negative

Specification

string Meter2Feet( float meter )
{
    string sign = "";
    if (meter < 0)
    {    // Use only positive value to simplify the maths
        sign = "-";
        meter = -meter;
    }
    
    float fraction = meter * 3.2808398950131233595800524934383;    // Compute feet multiplying by reciprocal value 
    integer feet = (integer)fraction;      // of conversion constant 0.3048 for performance reasons
    
    fraction = (fraction - feet) * 12;    // Compute inches
    integer inches = (integer)fraction;
    
    fraction = llRound((fraction - inches) * 16);    // Compute 1/16 inches
        
    string strFraction = "\"";
    if (fraction != 0)    // show fraction value only if not zero
        strFraction = " " + (string)((integer)fraction) + "/16" + strFraction;

    return (sign + (string)feet + "' " + (string)inches + strFraction);
}

Example

Trivial example to listen to any chat from the object owner for meter values and respond feet/inch value.

string Meter2Feet( float meter )
{
    string sign = "";
    if (meter < 0)
    {    // Use only positive value to simplify the maths
        sign = "-";
        meter = -meter;
    }
    
    float fraction = meter * 3.2808398950131233595800524934383;    // Compute feet multiplying by reciprocal value 
    integer feet = (integer)fraction;      // of conversion constant 0.3048 for performance reasons
    
    fraction = (fraction - feet) * 12;    // Compute inches
    integer inches = (integer)fraction;
    
    fraction = llRound((fraction - inches) * 16);    // Compute 1/16 inches
        
    string strFraction = "\"";
    if (fraction != 0)    // show fraction value only if not zero
        strFraction = " " + (string)((integer)fraction) + "/16" + strFraction;

    return (sign + (string)feet + "' " + (string)inches + strFraction);
}

default
{
    state_entry()
    {
        llOwnerSay("Enter: [m]");
        llListen(0, "", llGetOwner(), "");
    }
    
    listen(integer _chan, string _str, key _id, string _msg)
    {
        float meter = (float)_msg;
        llOwnerSay(Meter2Feet(meter));
    }
}}