From 2486840818ddbb5e8a54f2ba4584b98520ea221a Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 5 Oct 2014 20:39:33 +0200 Subject: [PATCH] data: Use %g format when formatting decimal numbers. This is much better human-readable and we don't need an exact representation. In fact, given that a datum's string value is used when comparing attribute values, the human readable format is much better suited given that chances are higher that users will enter numbers in the same format (but it's still just a best guess kind of thing). --- src/core/data.c | 4 ++-- t/unit/core/data_test.c | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/core/data.c b/src/core/data.c index eeead55..48ca6b0 100644 --- a/src/core/data.c +++ b/src/core/data.c @@ -380,7 +380,7 @@ sdb_data_strlen(const sdb_data_t *datum) /* log(64) */ return 20; case SDB_TYPE_DECIMAL: - /* XXX: -0xN.NNNNNNp+NNN */ + /* XXX: -d.dddddde+dd or -ddddd.dddddd */ return 42; case SDB_TYPE_STRING: if (! datum->data.string) @@ -416,7 +416,7 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted) ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer); break; case SDB_TYPE_DECIMAL: - ret = snprintf(buf, buflen, "%a", datum->data.decimal); + ret = snprintf(buf, buflen, "%g", datum->data.decimal); break; case SDB_TYPE_STRING: if (! datum->data.string) diff --git a/t/unit/core/data_test.c b/t/unit/core/data_test.c index 6f9376f..484d59c 100644 --- a/t/unit/core/data_test.c +++ b/t/unit/core/data_test.c @@ -533,7 +533,11 @@ START_TEST(test_format) }, { { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } }, - "0x1p+16", + "65536", + }, + { + { SDB_TYPE_DECIMAL, { .decimal = 12.3 } }, + "12.3", }, { { SDB_TYPE_STRING, { .string = NULL } }, -- 2.30.2