From b7de125bba74b23cec0e49189302655371d3e982 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Jun 2019 10:05:30 +0900 Subject: [PATCH] util: make format_bytes() support e.g. 3.0E --- src/basic/format-util.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/basic/format-util.c b/src/basic/format-util.c index 66551f7526..4231fa8d3f 100644 --- a/src/basic/format-util.c +++ b/src/basic/format-util.c @@ -32,7 +32,7 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) { { "K", UINT64_C(1000) }, }; const suffix_table *table; - size_t i; + size_t n, i; assert_cc(ELEMENTSOF(table_iec) == ELEMENTSOF(table_non_iec)); @@ -40,16 +40,19 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) { return NULL; table = flag & FORMAT_BYTES_USE_IEC ? table_iec : table_non_iec; + n = ELEMENTSOF(table_iec); - for (i = 0; i < ELEMENTSOF(table_iec); i++) + for (i = 0; i < n; i++) if (t >= table[i].factor) { - if (flag & FORMAT_BYTES_BELOW_POINT) + if (flag & FORMAT_BYTES_BELOW_POINT) { snprintf(buf, l, "%" PRIu64 ".%" PRIu64 "%s", t / table[i].factor, - ((t*UINT64_C(10)) / table[i].factor) % UINT64_C(10), + i != n - 1 ? + (t / table[i + 1].factor * UINT64_C(10) / table[n - 1].factor) % UINT64_C(10): + (t * UINT64_C(10) / table[i].factor) % UINT64_C(10), table[i].suffix); - else + } else snprintf(buf, l, "%" PRIu64 "%s", t / table[i].factor,