Browse Source

Save space, drop unuseful trailing zeroes

tags/json-c-0.11-20130402
Remi Collet 12 years ago
parent
commit
8c847968c7
1 changed files with 13 additions and 1 deletions
  1. +13
    -1
      json_object.c

+ 13
- 1
json_object.c View File

@@ -552,13 +552,25 @@ static int json_object_double_to_json_string(struct json_object* jso,
int level, int level,
int flags) int flags)
{ {
char buf[128], *p;
char buf[128], *p, *q;
int size; int size;


size = snprintf(buf, 128, "%f", jso->o.c_double); size = snprintf(buf, 128, "%f", jso->o.c_double);
p = strchr(buf, ','); p = strchr(buf, ',');
if (p) { if (p) {
*p = '.'; *p = '.';
} else {
p = strchr(buf, '.');
}
if (p) {
/* last useful digit, always keep 1 zero */
p++;
for (q=p ; *q ; q++) {
if (*q!='0') p=q;
}
/* drop trailing zeroes */
*(++p) = 0;
size = p-buf;
} }
printbuf_memappend(pb, buf, size); printbuf_memappend(pb, buf, size);
return size; return size;


Loading…
Cancel
Save