Difference between revisions of "Meter2Feet"
Jump to navigation
Jump to search
Huney Jewell (talk | contribs) m (1 ft = 0.3048m, see here: http://en.wikipedia.org/wiki/Foot_%28unit_of_length%29) |
Huney Jewell (talk | contribs) (Restored considering negative meter values) |
||
Line 14: | Line 14: | ||
<pre> | <pre> | ||
string Meter2Feet( float meter ) | string Meter2Feet( float meter ) | ||
string Meter2Feet (float meter) | |||
{ | { | ||
integer negative = FALSE; | |||
if (meter < 0) | |||
{ // Use only positive value to simplify the maths | |||
negative = TRUE; | |||
meter *= -1; | |||
} | |||
float fraction = meter / 0.3048; // Compute feet | float fraction = meter / 0.3048; // Compute feet | ||
integer feet = ( | integer feet = llFloor(fraction); | ||
fraction = (fraction - feet) * 12; // Compute inches | fraction = (fraction - feet) * 12; // Compute inches | ||
integer inches = ( | integer inches = llFloor(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 | |||
if ( | strFeet = "-" + strFeet; | ||
string strFraction = "\""; | |||
if (fraction != 0) // show fraction value only if not zero | |||
strFraction = " " + (string)(integer)fraction + "/16" + strFraction; | |||
return (strFeet + "' " + (string)inches + strFraction); | |||
} | } | ||
</pre> | </pre> | ||
Line 37: | Line 49: | ||
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; | |||
if (meter < 0) | |||
{ // Use only positive value to simplify the maths | |||
negative = TRUE; | |||
meter *= -1; | |||
} | |||
float fraction = meter / 0.3048; // Compute feet | float fraction = meter / 0.3048; // Compute feet | ||
integer feet = ( | integer feet = llFloor(fraction); | ||
fraction = (fraction - feet) * 12; // Compute inches | fraction = (fraction - feet) * 12; // Compute inches | ||
integer inches = ( | integer inches = llFloor(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 | |||
if ( | strFeet = "-" + strFeet; | ||
string strFraction = "\""; | |||
if (fraction != 0) // show fraction value only if not zero | |||
strFraction = " " + (string)(integer)fraction + "/16" + strFraction; | |||
return (strFeet + "' " + (string)inches + strFraction); | |||
} | } | ||
Line 67: | Line 90: | ||
llOwnerSay(Meter2Feet(meter)); | llOwnerSay(Meter2Feet(meter)); | ||
} | } | ||
}</pre> | |||
</pre> | |||
</div></div> | </div></div> | ||
{{LSLC|Examples|Meter2Feet}} | {{LSLC|Examples|Meter2Feet}} |
Revision as of 07:33, 12 September 2007
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Specification
string Meter2Feet( float meter ) string Meter2Feet (float meter) { integer negative = FALSE; if (meter < 0) { // 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 integer inches = llFloor(fraction); 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 = "\""; if (fraction != 0) // show fraction value only if not zero strFraction = " " + (string)(integer)fraction + "/16" + strFraction; return (strFeet + "' " + (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) { integer negative = FALSE; if (meter < 0) { // 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 integer inches = llFloor(fraction); 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 = "\""; if (fraction != 0) // show fraction value only if not zero strFraction = " " + (string)(integer)fraction + "/16" + strFraction; return (strFeet + "' " + (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)); } }