to_s()
public
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”.
Show source
/*
* 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);
}