Difference between revisions of "Float2String"

From Second Life Wiki
Jump to navigation Jump to search
(New page: {{LSL Header}} __NOTOC__ <div id="box"> == Function: string Float2String(float {{LSL Param|num}}, integer {{LSL Param|places}} , str {{LSL Param|rnd}}); == <div style="padd...)
 
m (<lsl> tag to <source>)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{LSL Header}} __NOTOC__
{{LSL Function
<div id="box">
|func=Float2String
== Function: [[string]] Float2String([[float]] {{LSL Param|num}}, [[integer]] {{LSL Param|places}} , [[str]] {{LSL Param|rnd}}); ==
|mode=user
<div style="padding: 0.5em;">
|p1_type=float|p1_name=num|p1_desc=number to be formatted
Allows string output of a float in a tidy text format, with optional rounding.
|p2_type=integer|p2_name=places|p2_desc=number of places to use during formatting
 
|p3_type=integer|p3_name=rnd|p3_desc=boolean, enables/disables rounding
Negative floats are fine (i.e. negative signs will be preserved.)<br />
|return_type=string
|return_text=that contains the float '''num''' in a tidy text format, with optional rounding.
|func_footnote=Negative floats are fine (i.e. negative signs will be preserved.)<br />
If you say 1 place, you will get 7.0 instead of 7.0000000<br />
If you say 1 place, you will get 7.0 instead of 7.0000000<br />
If you say 3 places, you get 7.14 instead of 7.1400000<br />
If you say 3 places, you get 7.14 instead of 7.1400000<br />
7.1373699 will come out as 7.13 or 7.14, depending on whether you specify rounding<br />
7.1373699 will come out as 7.13 or 7.14, depending on whether you specify rounding<br />
rnd (rounding) should be set to "Rounded" for Yes, "" for not rounded<br />
rnd (rounding) should be set to [[TRUE]] for Rounding, [[FALSE]] for no rounded<br />
 
Example:<br />
 
string myFormattedFloat = Float2String(-7.1373699,3,"Round");<br /> //rounded
returns -7.14
 
string myFormattedFloat = Float2String(-7.1373699,3,"");<br /> //not rounded
returns -7.13


'''Example:'''
<source lang="lsl2">
string myFormattedFloat = Float2String(-7.1373699, 3, TRUE);<br /> //rounded
//returns "-7.14"


</div></div>
string myFormattedFloat = Float2String(-7.1373699, 3, FALSE);<br /> //not rounded
<div id="box">
//returns "-7.13"
== Specification ==
</source>
<div style="padding: 0.5em;">
|spec=<source lang="lsl2">string Float2String ( float num, integer places, integer rnd) {  
<lsl>string Float2String ( float num, integer places, string rnd) {  
//allows string output of a float in a tidy text format
//allows string output of a float in a tidy text format
//rnd (rounding) should be set to "Rounded" for Yes, "" for not rounded
//rnd (rounding) should be set to TRUE for rounding, FALSE for no rounding


    string neg = "";
     if (rnd) {
     if (num < 0.0) neg = "-";
    if (places == 0) {
        return neg + (string) ((integer)num );
    }
    else if (rnd != "Yes") {
        if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000) {
            return llGetSubString((string)num, 0, places);
        }
        return (string)num;
    }
    else {
         float f = llPow( 10.0, places );
         float f = llPow( 10.0, places );
         integer i = llRound(llFabs(num) * f);
         integer i = llRound(llFabs(num) * f);
         string s = "00000" + (string)i; // number of 0s is (value of max places - 1 )
         string s = "00000" + (string)i; // number of 0s is (value of max places - 1 )
         return neg + (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
         if(num < 0.0)
            return "-" + (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
        return (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
     }
     }
}</lsl>
    if (!places)
</div></div>
        return (string)((integer)num );
 
    if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000)
Inspired by (and evolved from) discussions between Cheree Bury & Domino Marama in the SL Scripting Forum July 2008 at http://forums.secondlife.com/showthread.php?t=267884, with optional rounding added (that part being inspired by work by various authors here: https://wiki.secondlife.com/wiki/Fixed_Precision )
        return llGetSubString((string)num, 0, places);
 
    return (string)num;
{{LSLC|Examples|Float2String}}
}</source>
|deepnotes=Inspired by (and evolved from) discussions between {{User|Cheree Bury}} & {{User|Domino Marama}} in the SL Scripting Forum July 2008 at http://forums-archive.secondlife.com/54/15/267884/1.html, with optional rounding added (that part being inspired by work by various authors here: [[Fixed_Precision]] )
|cat1=Examples
}}

Latest revision as of 14:58, 24 January 2015

Summary

Function: string Float2String( float num, integer places, integer rnd );

Returns a string that contains the float num in a tidy text format, with optional rounding.

• float num number to be formatted
• integer places number of places to use during formatting
• integer rnd boolean, enables/disables rounding

Negative floats are fine (i.e. negative signs will be preserved.)
If you say 1 place, you will get 7.0 instead of 7.0000000
If you say 3 places, you get 7.14 instead of 7.1400000
7.1373699 will come out as 7.13 or 7.14, depending on whether you specify rounding
rnd (rounding) should be set to TRUE for Rounding, FALSE for no rounded

Example:

string myFormattedFloat = Float2String(-7.1373699, 3, TRUE);<br /> //rounded
//returns "-7.14"

string myFormattedFloat = Float2String(-7.1373699, 3, FALSE);<br /> //not rounded
//returns "-7.13"

Specification

string Float2String ( float num, integer places, integer rnd) { 
//allows string output of a float in a tidy text format
//rnd (rounding) should be set to TRUE for rounding, FALSE for no rounding

    if (rnd) {
        float f = llPow( 10.0, places );
        integer i = llRound(llFabs(num) * f);
        string s = "00000" + (string)i; // number of 0s is (value of max places - 1 )
        if(num < 0.0)
            return "-" + (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
        return (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
    }
    if (!places)
        return (string)((integer)num );
    if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000)
        return llGetSubString((string)num, 0, places);
    return (string)num;
}

Examples

Deep Notes

Inspired by (and evolved from) discussions between Cheree Bury & Domino Marama in the SL Scripting Forum July 2008 at http://forums-archive.secondlife.com/54/15/267884/1.html, with optional rounding added (that part being inspired by work by various authors here: Fixed_Precision )

Signature