Code

data: Use %g format when formatting decimal numbers.
authorSebastian Harl <sh@tokkee.org>
Sun, 5 Oct 2014 18:39:33 +0000 (20:39 +0200)
committerSebastian Harl <sh@tokkee.org>
Sun, 5 Oct 2014 18:39:33 +0000 (20:39 +0200)
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
t/unit/core/data_test.c

index eeead556899382b1e78270714a3e69eadc349129..48ca6b0bd74b9a4507c5a86b1c161d066d0f2cf7 100644 (file)
@@ -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)
index 6f9376ff48fdd5ad4a22b247bca20c6b5683dc41..484d59c64f9f10920648b027bac69dfa8002954a 100644 (file)
@@ -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 } },