public Method

Float.to_s

flt.to_s     string

Returns a string containing a representation of self. As well as a fixed or exponential form of the number, the call may return ``NaN’’, ``Infinity’’, and ``-Infinity’’.

Source Code

/*
*  call-seq:
*     flt.to_s    => string
*  
*  Returns a string containing a representation of self. As well as a
*  fixed or exponential form of the number, the call may return
*  ``<code>NaN</code>'', ``<code>Infinity</code>'', and
*  ``<code>-Infinity</code>''.
*/

static VALUE
flo_to_s(flt)
   VALUE flt;
{
   char buf[32];
   double value = RFLOAT(flt)->value;
   char *p, *e;

   if (isinf(value))
       return rb_str_new2(value < 0 ? "-Infinity" : "Infinity");
   else if(isnan(value))
       return rb_str_new2("NaN");

   sprintf(buf, "%#.15g", value); /* ensure to print decimal point */
   if (!(e = strchr(buf, 'e'))) {
       e = buf + strlen(buf);
   }
   if (!ISDIGIT(e[-1])) { /* reformat if ended with decimal point (ex 111111111111111.) */
       sprintf(buf, "%#.14e", value);
       if (!(e = strchr(buf, 'e'))) {
           e = buf + strlen(buf);
       }
   }
   p = e;
   while (p[-1]=='0' && ISDIGIT(p[-2]))
       p--;
   memmove(p, e, strlen(e)+1);
   return rb_str_new2(buf);
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.