Difference between revisions of "NumberFormat"

From Second Life Wiki
Jump to navigation Jump to search
 
m (<lsl> tag to <source>)
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
=== ''string'' '''llNumberFormat'''(''integer'') ===
{{LSL Header}}
<pre>
 
string llNumberFormat(integer number)
=== ''string'' '''NumberFormat'''(''integer'') ===
You can use this to take large numbers and format them to be comma separated by thousands.
<source lang="lsl2">
string NumberFormat(integer number)
{
{
     string output;     
     string output;     
     integer x;
     integer x = 0;
    integer z;
     string numberString = (string)number;
     string numberString = (string)number;
     integer numberStringLength = llStringLength(numberString);
     integer numberStringLength = llStringLength(numberString);
    integer z = (numberStringLength + 2) % 3;
      
      
    if ((numberStringLength % 3) == 2) z = 1;
     for(;x < numberStringLength; ++x)
    else if ((numberStringLength % 3) == 0) z = 2;
   
     for(x=0;x<numberStringLength;x++)
     {
     {
         output += llGetSubString(numberString, x, x);
         output += llGetSubString(numberString, x, x);
Line 21: Line 21:
     return output;
     return output;
}
}
</pre>
</source>
 
{{LSLC|Examples}}
{{#vardefine:sort|NumberFormat}}

Latest revision as of 16:32, 24 January 2015

string NumberFormat(integer)

You can use this to take large numbers and format them to be comma separated by thousands.

string NumberFormat(integer number)
{
    string output;    
    integer x = 0;
    string numberString = (string)number;
    integer numberStringLength = llStringLength(numberString);
    integer z = (numberStringLength + 2) % 3;
    
    for(;x < numberStringLength; ++x)
    {
        output += llGetSubString(numberString, x, x);
        if ((x % 3) == z && x != (numberStringLength - 1))
            output += ",";
    }
    
    return output;
}