From 7b22c44ccd24e7b0b3033803e9117a7da27f2fd3 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 22 Mar 2012 18:14:32 +0100 Subject: [PATCH] Result of INCRBYFLOAT and HINCRBYFLOAT should never be in exponential form, and also should never contain trailing zeroes. This is not possible with vanilla printf() format specifiers, so we alter the output. --- src/object.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/object.c b/src/object.c index ccb07208..9699ea53 100644 --- a/src/object.c +++ b/src/object.c @@ -48,7 +48,7 @@ robj *createStringObjectFromLongLong(long long value) { /* Note: this function is defined into object.c since here it is where it * belongs but it is actually designed to be used just for INCRBYFLOAT */ robj *createStringObjectFromLongDouble(long double value) { - char buf[256]; + char buf[256], *p; int len; /* We use 17 digits precision since with 128 bit floats that precision @@ -56,7 +56,16 @@ robj *createStringObjectFromLongDouble(long double value) { * that is "non surprising" for the user (that is, most small decimal * numbers will be represented in a way that when converted back into * a string are exactly the same as what the user typed.) */ - len = snprintf(buf,sizeof(buf),"%.17Lg", value); + len = snprintf(buf,sizeof(buf),"%.17Lf", value); + /* Now remove trailing zeroes after the '.' */ + if ((p = strchr(buf,'.')) != NULL) { + p = buf+len-1; + while(*p == '0') { + p--; + len--; + } + if (*p == '.') len--; + } return createStringObject(buf,len); } -- 2.47.2