Format Decimal: Difference between revisions
Jump to navigation
Jump to search
uniformity seems like a good idea |
Huney Jewell (talk | contribs) Now the examples table is formatted to standard style and entries are aligned correctly |
||
| Line 50: | Line 50: | ||
} | } | ||
</pre> | </pre> | ||
{| border="2" cellspacing="0" cellpadding="3" rules="all" style="margin:1em 1em 1em 0; border:solid 1px #AAAAAA; border-collapse:collapse; background-color:#F9F9F9; font-size:90%; empty-cells:show | '''Examples:''' | ||
{| border="2" cellspacing="0" cellpadding="3" rules="all" style="margin:1em 1em 1em 0; 0;border:solid 1px #AAAAAA; border-collapse:collapse; background-color:#F9F9F9; font-size:90%; empty-cells:show;" | |||
|-{{Hl2}} | |-{{Hl2}} | ||
!{{LSL Param|number}} | !{{LSL Param|number}} | ||
| Line 58: | Line 58: | ||
!Code | !Code | ||
|- | |- | ||
|123.456 | |style="text-align: right;" |123.456 | ||
|2 | |style="text-align: center;" |2 | ||
|123.45 | |style="text-align: right;" |123.45 | ||
|<code>FormatDecimal(123.456, 2)</code> | |<code>FormatDecimal(123.456, 2)</code> | ||
|- | |- | ||
| -123.456 | |style="text-align: right;" | -123.456 | ||
|2 | |style="text-align: center;" |2 | ||
| -123.45 | |style="text-align: right;" | -123.45 | ||
|<code>FormatDecimal(-123.456, 2)</code> | |<code>FormatDecimal(-123.456, 2)</code> | ||
|- | |- | ||
|123.456 | |style="text-align: right;" |123.456 | ||
|0 | |style="text-align: center;" |0 | ||
|123 | |style="text-align: right;" |123 | ||
|<code>FormatDecimal(123.456, 0)</code> | |<code>FormatDecimal(123.456, 0)</code> | ||
|- | |- | ||
|123.456 | |style="text-align: right;" |123.456 | ||
| -2 | |style="text-align: center;" | -2 | ||
|100 | |style="text-align: right;" |100 | ||
|<code>FormatDecimal(123.456, -2)</code> | |<code>FormatDecimal(123.456, -2)</code> | ||
|} | |} | ||
Revision as of 07:00, 10 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 |
| precision | Value |
|---|---|
| positive | Truncates number rounded to this fraction positions |
| zero | Truncates number rounded to it's integer value |
| negative | Rounds number at this pre-decimal position |
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:
| number | precision | Result | Code |
|---|---|---|---|
| 123.456 | 2 | 123.45 | FormatDecimal(123.456, 2)
|
| -123.456 | 2 | -123.45 | FormatDecimal(-123.456, 2)
|
| 123.456 | 0 | 123 | FormatDecimal(123.456, 0)
|
| 123.456 | -2 | 100 | FormatDecimal(123.456, -2)
|
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));
}
}