Talk:Float2String

From Second Life Wiki
Jump to: navigation, search

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

  1. Code gives syntax error when copying/pasting code. (superfluous "else"?)
  1. Float2String(-123.456,0,FALSE) yields "--123". Should be "-123".
  1. Float2String(-123.456,0,TRUE) yields "-123.00000123". Should be "-123"

--Rhian Svenska 16:11, 8 October 2008 (PDT)

I think I've fixed all but the last. -- Strife (talk|contribs) 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 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
       return (string)( (integer)(i / f) ) + "." + llGetSubString( (string)i, -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)

I should probably note also that the following is a tiny bit more efficient (less returns, which are fairly byte code heavy):

<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 str = "";
   if (rnd) {
       float f = llPow( 10.0, places );
       integer i = llRound(num * f); // Removed llFabs() due to later change
       str = (string)( (integer)(i / f) ) + "." + llGetSubString( (string)i, -places, -1); // Removed if (num < 0.0) as (string)((integer)i / f)) provides negative just fine
   } else if (!places) {
       str = (string)((integer)num );
   } else if if ( (places = (places - 7 - (places < 1) ) ) & 0x80000000) {
       str = llGetSubString((string)num, 0, places);
   } else {
       str = (string)num;
   }
   return str;

}</lsl>

I can't help but feel that there must be a clever way to factor out one of the two llGetSubString() calls, to save on function-calling byte code, but I can't think of it.
-- Haravikk (talk|contribs) 15:40, 21 May 2012 (PDT)