Difference between revisions of "Float2String"

From Second Life Wiki
Jump to navigation Jump to search
m
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}} , [[string]] {{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 />
Example:<br />


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


string myFormattedFloat = Float2String(-7.1373699,3,"");<br /> //not rounded
string myFormattedFloat = Float2String(-7.1373699, 3, FALSE);<br /> //not rounded
returns -7.13
returns -7.13
 
|spec=<lsl>string Float2String ( float num, integer places, integer rnd) {  
 
</div></div>
<div id="box">
== Specification ==
<div style="padding: 0.5em;">
<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 = "";
     string neg = "";
Line 33: Line 29:
         return neg + (string) ((integer)num );
         return neg + (string) ((integer)num );
     }
     }
     else if (rnd != "Yes") {
     else 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 )
        return neg + (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
    }
    else {
         if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000) {
         if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000) {
             return llGetSubString((string)num, 0, places);
             return llGetSubString((string)num, 0, places);
Line 39: Line 41:
         return (string)num;
         return (string)num;
     }
     }
     else {
     else
        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 )
        return neg + (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
    }
}</lsl>
}</lsl>
</div></div>
|deepnotes=Inspired by (and evolved from) discussions between {{User|Cheree Bury}} & {{User|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: [[Fixed_Precision]] )
 
|cat1=Examples
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 )
}}
 
{{LSLC|Examples|Float2String}}

Revision as of 07:19, 18 September 2008

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);
//rounded returns -7.14

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

Specification

<lsl>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

   string neg = "";
   if (num < 0.0) neg = "-";
   if (places == 0) {
       return neg + (string) ((integer)num );
   }
   else 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 )
       return neg + (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1);
   }
   else {
       if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000) {
           return llGetSubString((string)num, 0, places);
       }
       return (string)num;
   }
   else

}</lsl>

Examples

Deep Notes

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: Fixed_Precision )

Signature