Difference between revisions of "Meter2Feet"

From Second Life Wiki
Jump to navigation Jump to search
m
m (<lsl> tag to <source>)
 
(8 intermediate revisions by 2 users not shown)
Line 6: Line 6:
Returns a [[String|string]] of metric value '''{{LSL Param|meter}}''' converted to feet/inch in common representation, ie. 3' 4 5/16".
Returns a [[String|string]] of metric value '''{{LSL Param|meter}}''' converted to feet/inch in common representation, ie. 3' 4 5/16".
{|
{|
{{LSL DefineRow|[[float]]|meter|{{#var:p_number_desc}}}}
{{LSL DefineRow|[[float]]|meter|{{#var:p_meter_desc}}}}
|}
|}
</div></div>
</div></div>
Line 12: Line 12:
== Specification ==
== Specification ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<pre>
<source lang="lsl2">
string Meter2Feet( float meter )
string Meter2Feet( float meter )
{
{
float fraction = meter / 0.3047999984375; // Compute feet
    string sign = "";
integer feet = (integer)fraction;
    if (meter < 0)
    {    // Use only positive value to simplify the maths
fraction = (fraction - feet) * 12; // Compute inches
        sign = "-";
integer inches = (integer)fraction;
        meter = -meter;
    }
fraction = llRound((fraction - inches) * 16); // Compute 1/16 inches
   
    // Compute feet multiplying by reciprocal value of
string strFraction = "";
    //  conversion constant 0.3048 for performance reasons
if (fraction) // show fraction value only if not zero
    float fraction = meter * 3.2808398950131233595800524934383;
strFraction = " " + (string)llAbs((integer)fraction) + "/16";
    integer feet = (integer)fraction;
   
return ((string)feet + "' " + (string)llAbs(inches) + strFraction+ "\"");
    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);
}
}
</pre>
</source>
</div></div>
</div></div>


Line 36: Line 45:
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Trivial example to listen to any chat from the object owner for '''{{LSL Param|meter}}''' values and respond feet/inch value.
Trivial example to listen to any chat from the object owner for '''{{LSL Param|meter}}''' values and respond feet/inch value.
<pre>
<source lang="lsl2">
string Meter2Feet( float meter )
// Insert code of Meter2Feet user function here
{
 
float fraction = meter / 0.3047999984375; // Compute feet
integer feet = (integer)fraction;
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)llAbs((integer)fraction) + "/16\"";
return ((string)feet + "' " + (string)llAbs(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));
}
    }
}</source>
touch_start(integer n)
{
llOwnerSay( Meter2Feet( llVecDist( llGetPos(), llDetectedPos(0))));
}
}
</pre>
</div></div>
</div></div>
{{LSLC|Examples|Meter2Feet}}
{{LSLC|Examples|Meter2Feet}}

Latest revision as of 16:19, 24 January 2015

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;
    }
    
    // Compute feet multiplying by reciprocal value of 
    //  conversion constant 0.3048 for performance reasons
    float fraction = meter * 3.2808398950131233595800524934383;
    integer feet = (integer)fraction;
    
    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.

// Insert code of Meter2Feet user function here


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));
    }
}