diff options
author | Alex Suykov <alex.suykov@gmail.com> | 2018-03-04 21:18:10 +0200 |
---|---|---|
committer | Arun Prakash Jana <engineerarun@gmail.com> | 2018-03-05 00:48:10 +0530 |
commit | 7654a2e0c6057c6d6d85e0625627e3e1dbe8d52d (patch) | |
tree | ba3de66b6c3a15c04a7e24f78095ca1cde5f93fa | |
parent | 3036b8a733fed06d83a2dca26d5e31de47438403 (diff) | |
download | nnn-7654a2e0c6057c6d6d85e0625627e3e1dbe8d52d.tar.gz |
fix rounding carry in coolsize() (#91)
Sizes like 1.999 get rounded to (1 + 100/100), which should be displayed
as 2.00 not 1.100 as simple %i.%02i would do without additional measures.
-rw-r--r-- | nnn.c | 11 |
1 files changed, 8 insertions, 3 deletions
@@ -1442,6 +1442,7 @@ coolsize(off_t size) static char size_buf[12]; /* Buffer to hold human readable size */ static int rem, i; static int fdig; /* number of fractional digits to show */ + static int frac; i = 0; rem = 0; @@ -1452,12 +1453,16 @@ coolsize(off_t size) ++i; } - rem = (1000 * rem) >> 10; /* convert 1024th fractions to 1000th */ fdig = 3; + frac = 1000; + rem = (10000 * rem) >> 10; /* convert 1024th fractions to 10000th */ + rem = (rem / 10) + (rem % 10 >= 5 ? 1 : 0); /* round to 1000th */ /* Show 1 decimal for KB sizes and 2 decimals for MBs. */ - if (i < 3) { --fdig; rem = (rem + 5) / 10; } - if (i < 2) { --fdig; rem = (rem + 5) / 10; } + if (i < 3) { --fdig; rem = (rem + 5) / 10; frac /= 10; } + if (i < 2) { --fdig; rem = (rem + 5) / 10; frac /= 10; } + /* carry, in case rounding above overflows the fractional part */ + if (i < 3 && rem >= frac) { size++; rem = 0; } if (i > 0) snprintf(size_buf, 12, "%" PRId64 ".%0*i%c", size, fdig, rem, U[i]); |