lib/strutils: support two decimal places in size_to_human_string() output

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2019-02-12 13:27:56 +01:00
parent 3b9dc305ee
commit 07b94c9f32
2 changed files with 15 additions and 7 deletions

View 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);

View 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) {