summaryrefslogtreecommitdiff
path: root/xs_json.h
diff options
context:
space:
mode:
authordefault <nobody@localhost>2022-09-25 22:40:09 +0200
committerdefault <nobody@localhost>2022-09-25 22:40:09 +0200
commit6d33117dbe062474c89a711d02f30e07e4bb1633 (patch)
treedb606bb1dcef04f0a7f727f9401d878826cfc655 /xs_json.h
parenta63c9c24c1b3f6c46ff1bc7a3428e8f50b4b1318 (diff)
Remove manually the leading zeros in _xs_json_dumps().
Diffstat (limited to 'xs_json.h')
-rw-r--r--xs_json.h15
1 files changed, 14 insertions, 1 deletions
diff --git a/xs_json.h b/xs_json.h
index 5280b14..8aa3f16 100644
--- a/xs_json.h
+++ b/xs_json.h
@@ -93,7 +93,20 @@ d_char *_xs_json_dumps(d_char *s, char *data, int level, int indent)
case XSTYPE_NUMBER:
{
char tmp[32];
- snprintf(tmp, sizeof(tmp), "%g", xs_number_get(data));
+ snprintf(tmp, sizeof(tmp), "%.15f", xs_number_get(data));
+
+ /* strip useless zeros */
+ if (strchr(tmp, '.') != NULL) {
+ char *ptr;
+
+ for (ptr = tmp + strlen(tmp) - 1; *ptr == '0'; ptr--);
+
+ if (*ptr != '.')
+ ptr++;
+
+ *ptr = '\0';
+ }
+
s = xs_str_cat(s, tmp);
}
break;