]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: support two decimal places in size_to_human_string() output
authorKarel Zak <kzak@redhat.com>
Tue, 12 Feb 2019 12:27:56 +0000 (13:27 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 12 Feb 2019 13:20:55 +0000 (14:20 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h
lib/strutils.c

index 0c46798821471300fb1212fbcd6597893cf54155..65d5259db0820d04617f82ee9ec72936508d8495 100644 (file)
@@ -121,9 +121,10 @@ extern char *xstrmode(mode_t mode, char *str);
 /* Options for size_to_human_string() */
 enum
 {
-        SIZE_SUFFIX_1LETTER = 0,
-        SIZE_SUFFIX_3LETTER = 1,
-        SIZE_SUFFIX_SPACE   = 2
+       SIZE_SUFFIX_1LETTER  = 0,
+       SIZE_SUFFIX_3LETTER  = (1 << 0),
+       SIZE_SUFFIX_SPACE    = (1 << 1),
+       SIZE_DECIMAL_2DIGITS = (1 << 2)
 };
 
 extern char *size_to_human_string(int options, uint64_t bytes);
index b71dde596c0f1aadd01d17a57c85c211d94faa35..369d5015912e6c30db5afe702cfe40d1df0287bf 100644 (file)
@@ -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) {