aboutsummaryrefslogtreecommitdiffstats
path: root/nnn.c
diff options
context:
space:
mode:
authorGravatar Arun Prakash Jana <engineerarun@gmail.com>2018-03-05 01:12:10 +0530
committerGravatar Arun Prakash Jana <engineerarun@gmail.com>2018-03-05 01:12:10 +0530
commit4d93a7f9fe4659fe0852ab56ab3ff9ab40dff7bf (patch)
tree9df3b57892eff803f04c3ab8d301a493e9c72771 /nnn.c
parent58d90bfcf68441b9fdf659788d92a257a1597deb (diff)
downloadnnn-4d93a7f9fe4659fe0852ab56ab3ff9ab40dff7bf.tar.gz
Revert "Integer-only coolsize() (#84)"
Diffstat (limited to 'nnn.c')
-rw-r--r--nnn.c20
1 files changed, 5 insertions, 15 deletions
diff --git a/nnn.c b/nnn.c
index 0c4c695..5568574 100644
--- a/nnn.c
+++ b/nnn.c
@@ -80,7 +80,6 @@
#endif
#include <ftw.h>
#include <wchar.h>
-#include <inttypes.h>
#include "nnn.h"
@@ -1440,8 +1439,10 @@ coolsize(off_t size)
{
static const char * const U = "BKMGTPEZY";
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 i;
+
+ static long double rem;
+ static const double div_2_pow_10 = 1.0 / 1024.0;
i = 0;
rem = 0;
@@ -1452,18 +1453,7 @@ coolsize(off_t size)
++i;
}
- rem = (1000 * rem) >> 10; /* convert 1024th fractions to 1000th */
- fdig = 3;
-
- /* 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 > 0)
- snprintf(size_buf, 12, "%" PRId64 ".%0*i%c", size, fdig, rem, U[i]);
- else
- snprintf(size_buf, 12, "%" PRId64 "%c", size, U[i]);
-
+ snprintf(size_buf, 12, "%.*Lf%c", i, size + rem * div_2_pow_10, U[i]);
return size_buf;
}