Difference between revisions of "Format Decimal"
Jump to navigation
Jump to search
m (^^; oops) |
|||
Line 1: | Line 1: | ||
{{LSL Header}} | {{LSL Header}}__NOTOC__ | ||
<div id="box"> | |||
== Function: [[string]] FormatDecimal([[float]] number, [[integer]] precision); == | == Function: [[string]] FormatDecimal([[float]] number, [[integer]] precision); == | ||
<div style="padding: 0.5em;"> | |||
''Formats a float value to a fixed precision ie. for display purposes'' | ''Formats a float value to a fixed precision ie. for display purposes'' | ||
Returns a [[String|string]] that comprises the '''number''' truncated and rounded to the given '''precision'''. | Returns a [[String|string]] that comprises the '''number''' truncated and rounded to the given '''precision'''. | ||
{ | {{{!}} | ||
{{LSL DefineRow|[[float]]|number|any valid float, positive or negative}} | {{LSL DefineRow|[[float]]|number|any valid float, positive or negative}} | ||
{{LSL DefineRow|[[integer]]|precision|precision of result. Positive values truncate fraction, negative values truncate integer part of '''number'''}} | {{LSL DefineRow|[[integer]]|precision|precision of result. Positive values truncate fraction, negative values truncate integer part of '''number'''}} | ||
{{!}}} | |||
</div></div> | |||
<div id="box"> | |||
== Specification == | |||
<div style="padding: 0.5em;"> | |||
<pre> | <pre> | ||
string FormatDecimal (float number, integer precision) | string FormatDecimal (float number, integer precision) | ||
Line 32: | Line 38: | ||
<br/>FormatDecimal(123.456, 0); // Result: 123 | <br/>FormatDecimal(123.456, 0); // Result: 123 | ||
<br/>FormatDecimal(-123.456, -2); // Result: -100 | <br/>FormatDecimal(-123.456, -2); // Result: -100 | ||
</div></div><div id="box"> | |||
== Caveats == | == Caveats == | ||
Maximum value is restricted to [[integer]] range, that is -2,147,483,648 to +2,147,483,647 | <div style="padding: 0.5em;"> | ||
Maximum value is restricted to [[integer]] range, that is -2,147,483,648 to +2,147,483,647 | |||
</div></div> | |||
<div id="box"> | |||
== Example == | == Example == | ||
<div style="padding: 0.5em;"> | |||
Trivial example to listen to any chat from the object owner for ''number,precision'' values and respond formatted value. | Trivial example to listen to any chat from the object owner for ''number,precision'' values and respond formatted value. | ||
<pre> | <pre> | ||
string FormatDecimal (float number, integer precision) | |||
{ | |||
float roundingValue = llPow(10, -precision)*0.5; | |||
float rounded; | |||
if (number < 0) rounded = number - roundingValue; | |||
else rounded = number + roundingValue; | |||
if (precision < 1) // Rounding integer value | |||
{ | |||
integer intRounding = (integer)llPow(10, -precision); | |||
rounded = (integer)rounded/intRounding*intRounding; | |||
precision = -1; // Don't truncate integer value | |||
} | |||
string strNumber = (string)rounded; | |||
return llGetSubString(strNumber, 0, llSubStringIndex(strNumber, ".") + precision); | |||
} | |||
default | |||
{ | |||
state_entry() | |||
{ | |||
llOwnerSay("Enter: [number,precision]"); | |||
llListen(0, "", llGetOwner(), ""); | |||
} | |||
listen(integer _chan, string _str, key _id, string _msg) | |||
{ | |||
list values=llCSV2List(_msg); | |||
float number = llList2Float(values,0); | |||
integer decimals = llList2Integer(values,1); | |||
llOwnerSay("Number " + (string)number + " is formatted " + FormatDecimal(number, decimals)); | |||
} | |||
} | |||
</pre> | </pre> | ||
</div></div> | |||
{{LSLC|Examples|Format Decimal}} | {{LSLC|Examples|Format Decimal}} |
Revision as of 16:02, 9 September 2007
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Function: string FormatDecimal(float number, integer precision);
Formats a float value to a fixed precision ie. for display purposes
Returns a string that comprises the number truncated and rounded to the given precision.
• float | number | – | any valid float, positive or negative | |
• integer | precision | – | precision of result. Positive values truncate fraction, negative values truncate integer part of number |
Specification
string FormatDecimal (float number, integer precision) { float roundingValue = llPow(10, -precision)*0.5; float rounded; if (number < 0) rounded = number - roundingValue; else rounded = number + roundingValue; if (precision < 1) // Rounding integer value { integer intRounding = (integer)llPow(10, -precision); rounded = (integer)rounded/intRounding*intRounding; precision = -1; // Don't truncate integer value } string strNumber = (string)rounded; return llGetSubString(strNumber, 0, llSubStringIndex(strNumber, ".") + precision); }
Examples:
FormatDecimal(123.456, 2); // Result: 123.46
FormatDecimal(-123.456, 2); // Result: -123.46
FormatDecimal(123.456, 0); // Result: 123
FormatDecimal(-123.456, -2); // Result: -100
Caveats
Maximum value is restricted to integer range, that is -2,147,483,648 to +2,147,483,647
Example
Trivial example to listen to any chat from the object owner for number,precision values and respond formatted value.
string FormatDecimal (float number, integer precision) { float roundingValue = llPow(10, -precision)*0.5; float rounded; if (number < 0) rounded = number - roundingValue; else rounded = number + roundingValue; if (precision < 1) // Rounding integer value { integer intRounding = (integer)llPow(10, -precision); rounded = (integer)rounded/intRounding*intRounding; precision = -1; // Don't truncate integer value } string strNumber = (string)rounded; return llGetSubString(strNumber, 0, llSubStringIndex(strNumber, ".") + precision); } default { state_entry() { llOwnerSay("Enter: [number,precision]"); llListen(0, "", llGetOwner(), ""); } listen(integer _chan, string _str, key _id, string _msg) { list values=llCSV2List(_msg); float number = llList2Float(values,0); integer decimals = llList2Integer(values,1); llOwnerSay("Number " + (string)number + " is formatted " + FormatDecimal(number, decimals)); } }