Difference between revisions of "Format Decimal"

From Second Life Wiki
Jump to navigation Jump to search
(Added description of 'FormatDecimal' function)
 
m (<lsl> tag to <source>)
 
(15 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{LSL Header}}
{{LSL Header}}__NOTOC__
== Function: [[String|string]] FormatDecimal([[Float|float]] number, [[Integer|integer]] precision); ==
<div id="box">{{#vardefine:p_number_desc|any valid float, positive or negative
}}{{#vardefine:p_precision_desc|precision of result. Positive values truncate rounded fraction, negative values truncate rounded '''number''' to integer
}}
== Function: [[string]] FormatDecimal([[float]] {{LSL Param|number}}, [[integer]] {{LSL Param|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 '''{{LSL Param|number}}''' truncated and rounded to the given '''{{LSL Param|precision}}'''.
* [[Float|float]] number - any valid float, positive or negative
{|
* [[Integer|integer]] precision - precision of result. Positive values truncate fraction, negative values truncate integer part of '''number'''
{{LSL DefineRow|[[float]]|number|{{#var:p_number_desc}}}}
<pre>
{{LSL DefineRow|[[integer]]|precision|{{#var:p_precision_desc}}}}
string FormatDecimal (float number, integer precision)
|}
{     
<br/>
    float roundingValue = llPow(10, -precision)*0.5;
{| {{Prettytable}}
    float rounded;
|-{{Hl2}}
    if (number < 0) rounded = number - roundingValue;
!{{LSL Param|precision}}
    else            rounded = number + roundingValue;
!Value
   
|-
    if (precision < 1) // Rounding integer value
|positive
    {
|Truncates '''{{LSL Param|number}}''' rounded to this fraction positions
        integer intRounding = (integer)llPow(10, -precision);
|-
        rounded = (integer)rounded/intRounding*intRounding;
|zero
        precision = -1; // Don't truncate integer value
|Truncates '''{{LSL Param|number}}''' rounded to it's integer value
    }
|-
   
|negative
    string strNumber = (string)rounded;
|Rounds '''{{LSL Param|number}}''' at this pre-decimal position
    return llGetSubString(strNumber, 0, llSubStringIndex(strNumber, ".") + precision);
|}
}
</div></div>
</pre>
<div id="box">
== Specification ==
<div style="padding: 0.5em;">
<source lang="lsl2">
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);
}
</source>
'''Examples:'''
'''Examples:'''
<br/>FormatDecimal(123.456, 2); // Result: 123.46
{| 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;"
<br/>FormatDecimal(-123.456, 2); // Result: -123.46
|-{{Hl2}}
<br/>FormatDecimal(123.456, 0); // Result: 123
!{{LSL Param|number}}
<br/>FormatDecimal(-123.456, -2); // Result: -100
!{{LSL Param|precision}}
!Result
!Code
|-
|style="text-align: right;" |123.456
|style="text-align: center;" |2
|style="text-align: right;" |123.46
|<code>FormatDecimal(123.456, 2)</code>
|-
|style="text-align: right;" | -123.456
|style="text-align: center;" |2
|style="text-align: right;" | -123.46
|<code>FormatDecimal(-123.456, 2)</code>
|-
|style="text-align: right;" |123.456
|style="text-align: center;" |0
|style="text-align: right;" |123
|<code>FormatDecimal(123.456, 0)</code>
|-
|style="text-align: right;" |123.456
|style="text-align: center;" | -2
|style="text-align: right;" |100
|<code>FormatDecimal(123.456, -2)</code>
|}
</div></div><div id="box">


== Caveats ==
== Caveats ==
Maximum value is restricted to [[Integer|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 ==
<div style="padding: 0.5em;">
Trivial example to listen to any chat from the object owner for '''{{LSL Param|number}}, {{LSL Param|precision}}''' values and respond formatted value.
<source lang="lsl2">
// Insert code of FormatDecimal user function here


== Example ==
default
Trivial example to listen to any chat from the object owner for ''number,precision'' values and respond formatted value.
{
<pre>
    state_entry()
string FormatDecimal (float number, integer precision)
    {
{   
        llOwnerSay("Enter: [number,precision]");
    float roundingValue = llPow(10, -precision)*0.5;
        llListen(0, "", llGetOwner(), "");
    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>


    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));
    }
}
</source>
</div></div>
{{LSLC|Examples|Format Decimal}}
{{LSLC|Examples|Format Decimal}}

Latest revision as of 14:00, 24 January 2015

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 rounded fraction, negative values truncate rounded number to integer


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.46 FormatDecimal(123.456, 2)
-123.456 2 -123.46 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.

// Insert code of FormatDecimal user function here

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