From: "H.Yamamoto" Date: 2004-05-10T13:36:54+09:00 Subject: Re: 5.000...007.to_s => "5" Sorry for late posting. Does this patch solve your problem? * numeric.c(flo_to_s): use DBL_DIG to show only significant digits. Index: numeric.c =================================================================== RCS file: /ruby/ruby/numeric.c,v retrieving revision 1.107 diff -u -w -b -p -r1.107 numeric.c --- numeric.c 7 May 2004 08:44:15 -0000 1.107 +++ numeric.c 10 May 2004 04:00:45 -0000 @@ -489,10 +489,9 @@ static VALUE flo_to_s(flt) VALUE flt; { - char buf[32]; - char *fmt = "%.15f"; + char fmt[sizeof(int)*3+10], buf[sizeof(double)*3+sizeof(int)*3+20]; double value = RFLOAT(flt)->value; - double avalue, d1, d2; + int exp; char *p, *e; if (isinf(value)) @@ -500,13 +499,25 @@ flo_to_s(flt) else if(isnan(value)) return rb_str_new2("NaN"); - avalue = fabs(value); - if (avalue < 1.0e-7 || avalue >= 1.0e15) { - fmt = "%.16e"; - } + sprintf(fmt, "%%.%de", DBL_DIG - 1); sprintf(buf, fmt, value); - if (!(e = strchr(buf, 'e'))) { - e = buf + strlen(buf); + e = strchr(buf, 'e'); + exp = atoi(e+1); + if (-7 <= exp && exp < DBL_DIG - 1) { + char *d = strchr(buf, '.'); + if (exp > 0) { + memmove(d, d+1, exp); + d[exp] = '.'; + } + if (exp < 0) { /* needs room in `buf' for shifting (currently 7) */ + int n = -exp; + d[0] = d[-1]; + memmove(d+n, d, strlen(d)+1); + e += n; + memset(buf, '0', n+1); + buf[1] = '.'; + } + *e = '\0'; } p = e; while (*--p=='0')