Difference between revisions of "Meter2Feet"

From Second Life Wiki
Jump to navigation Jump to search
m
Line 1: Line 1:
{{LSL Header}}__NOTOC__
{{LSL Header}}__NOTOC__
<div id="box">
<div id="box">
{{#vardefine:p_number_desc|any valid float, positive or negative}}
{{#vardefine:p_meter_desc|distance in meters, any valid float, positive or negative}}
== Function: [[string]] Meter2Feet([[float]] {{LSL Param|meter}}); ==
== Function: [[string]] Meter2Feet([[float]] {{LSL Param|meter}}); ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Line 23: Line 23:
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) // show fraction value only if not zero
strFraction = " " + (string)llAbs((integer)fraction) + "/16\"";
strFraction = " " + (string)llAbs((integer)fraction) + "/16";
return ((string)feet + "' " + (string)llAbs(inches) + strFraction);
return ((string)feet + "' " + (string)llAbs(inches) + strFraction+ "\"");
}
}
</pre>
</pre>
Line 33: Line 33:


<div id="box">
<div id="box">
19.5071999
== Example ==
== Example ==
<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>
<pre>
string Meter2Feet (float meter)
string Meter2Feet( float meter )
{
{
integer negative = FALSE;
float fraction = meter / 0.3047999984375; // Compute feet
if (meter < 0)
integer feet = (integer)fraction;
{ // Use only positive value to simplify the maths
negative = TRUE;
meter *= -1;
}
float fraction = meter / 0.3048; // Compute feet
integer feet = llFloor(fraction);
fraction = (fraction - feet) * 12; // Compute inches
fraction = (fraction - feet) * 12; // Compute inches
integer inches = llFloor(fraction);
integer inches = (integer)fraction;
fraction = llRound((fraction - inches) * 16); // Compute 1/16 inches
fraction = llRound((fraction - inches) * 16); // Compute 1/16 inches
string strFeet = (string)feet;
if (negative) // Reallocate sign to ensure showing up in case feet value is zero
strFeet = "-" + strFeet;
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)llAbs((integer)fraction) + "/16\"";
return (strFeet + "' " + (string)inches + strFraction);
return ((string)feet + "' " + (string)llAbs(inches) + strFraction);
}
}


Line 73: Line 66:
float meter = (float)_msg;
float meter = (float)_msg;
llOwnerSay(Meter2Feet(meter));
llOwnerSay(Meter2Feet(meter));
}
touch_start(integer n)
{
llOwnerSay( Meter2Feet( llVecDist( llGetPos(), llDetectedPos(0))));
}
}
}
}

Revision as of 07:12, 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

Specification

string Meter2Feet( float meter )
{
	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)	// show fraction value only if not zero
		strFraction = " " + (string)llAbs((integer)fraction) + "/16";
	
	return ((string)feet + "' " + (string)llAbs(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 )
{
	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
{
	state_entry()
	{
		llOwnerSay("Enter: [m]");
		llListen(0, "", llGetOwner(), "");
	}
	
	listen(integer _chan, string _str, key _id, string _msg)
	{
		float meter = (float)_msg;
		llOwnerSay(Meter2Feet(meter));
	}
	
	touch_start(integer n)
	{
		llOwnerSay( Meter2Feet( llVecDist( llGetPos(), llDetectedPos(0))));
	}
}