diff options
author | Karel Zak | 2019-02-12 13:27:56 +0100 |
---|---|---|
committer | Karel Zak | 2019-02-12 14:20:55 +0100 |
commit | 07b94c9f327c0b3230022b55f8105249f205a739 (patch) | |
tree | 8b375ac074cd4f91a3a778b96035341c8b6b5276 /lib | |
parent | tests: (hardlink) update noregex (diff) | |
download | kernel-qcow2-util-linux-07b94c9f327c0b3230022b55f8105249f205a739.tar.gz kernel-qcow2-util-linux-07b94c9f327c0b3230022b55f8105249f205a739.tar.xz kernel-qcow2-util-linux-07b94c9f327c0b3230022b55f8105249f205a739.zip |
lib/strutils: support two decimal places in size_to_human_string() output
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/strutils.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/strutils.c b/lib/strutils.c index b71dde596..369d50159 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -551,6 +551,7 @@ char *size_to_human_string(int options, uint64_t bytes) if (options & SIZE_SUFFIX_SPACE) *psuf++ = ' '; + exp = get_exp(bytes); c = *(letters + (exp ? exp / 10 : 0)); dec = exp ? bytes / (1ULL << exp) : bytes; @@ -569,11 +570,17 @@ char *size_to_human_string(int options, uint64_t bytes) * exp, suffix[0], dec, frac); */ + /* round */ if (frac) { - /* round */ - frac = (frac / (1ULL << (exp - 10)) + 50) / 100; - if (frac == 10) - dec++, frac = 0; + if (options & SIZE_DECIMAL_2DIGITS) { + frac = (frac / (1ULL << (exp - 10)) + 5) / 10; + if (frac % 10 == 0) + frac /= 10; /* convert N.90 to N.9 */ + } else { + frac = (frac / (1ULL << (exp - 10)) + 50) / 100; + if (frac == 10) + dec++, frac = 0; + } } if (frac) { |