Difference between revisions of "Talk:Float2String"
(→More code than needed?: new section) |
|||
Line 35: | Line 35: | ||
:I think I've fixed all but the last. -- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 20:11, 8 October 2008 (PDT) | :I think I've fixed all but the last. -- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 20:11, 8 October 2008 (PDT) | ||
== More code than needed? == | |||
Forgive me if I'm wrong, but it looks to me as if the "00000" prefix for <code>s</code> and the call to [[llFabs]](), and subsequent <code>if (neg < 0.0)</code> are extraneous? In all my tests it seems the code functions just fine without any of them, am I missing something? | |||
Like-so: | |||
<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 | |||
if (rnd) { | |||
float f = llPow( 10.0, places ); | |||
integer i = llRound(num * f); // Removed llFabs() due to later change | |||
string s = (string)i; // number of 0s is (value of max places - 1 ) // Remove "00000" as it...well it doesn't do anything at all due to use of negative indices in llGetSubString? | |||
return (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1); // Removed if (num < 0.0) as (string)((integer)i / f)) provides negative just fine | |||
} | |||
if (!places) | |||
return (string)((integer)num ); | |||
if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000) | |||
return llGetSubString((string)num, 0, places); | |||
return (string)num; | |||
}</lsl><br/>-- '''[[User:Haravikk_Mistral|Haravikk]]''' <sup><small>([[User_talk:Haravikk_Mistral|talk]]|[[Special:Contributions/Haravikk_Mistral|contribs]])</small></sup> 22:01, 18 October 2010 (UTC) |
Revision as of 15:01, 18 October 2010
Please be kind to totally review your text and code; examples are false, code is insufficiently implemented.
Examples: string myFormattedFloat = Float2String(-7.1373699,3,"Round"); //rounded returns -7.14
string myFormattedFloat = Float2String(-7.1373699,3,""); //not rounded returns -7.13
The real result is -7.137 for the both
Code:
if (places == 0) { return neg + (string) ((integer)num ); }
This is true if you want to truncate it, but false if you want to round it.
Other: To round it's written : "should be set to "Rounded" for Yes" in the expanations, //rnd (rounding) should be set to "Rounded" for Yes, "" for not rounded in a Comment Line and Yes in the code. Note that the wiki is first for people who want to study LSL.
Garmin Kawaguichi
Errors with posted code
- Code gives syntax error when copying/pasting code. (superfluous "else"?)
- Float2String(-123.456,0,FALSE) yields "--123". Should be "-123".
- Float2String(-123.456,0,TRUE) yields "-123.00000123". Should be "-123"
--Rhian Svenska 16:11, 8 October 2008 (PDT)
More code than needed?
Forgive me if I'm wrong, but it looks to me as if the "00000" prefix for s
and the call to llFabs(), and subsequent if (neg < 0.0)
are extraneous? In all my tests it seems the code functions just fine without any of them, am I missing something?
Like-so: <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
if (rnd) { float f = llPow( 10.0, places ); integer i = llRound(num * f); // Removed llFabs() due to later change string s = (string)i; // number of 0s is (value of max places - 1 ) // Remove "00000" as it...well it doesn't do anything at all due to use of negative indices in llGetSubString? return (string)( (integer)(i / f) ) + "." + llGetSubString( s, -places, -1); // Removed if (num < 0.0) as (string)((integer)i / f)) provides negative just fine } if (!places) return (string)((integer)num ); if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000) return llGetSubString((string)num, 0, places); return (string)num;
}</lsl>
-- Haravikk (talk|contribs) 22:01, 18 October 2010 (UTC)