commit 4355e85ee8fe69dd8e514d64376db722e51fa04d
parent 1fcc476cb9cc887196dd597f86af5f5f8902a890
Author: Quentin Rameau <quinq.ml@gmail.com>
Date: Sat, 23 Jul 2016 13:11:29 +0200
Print more precision when scaling
We now print two decimals for a final format of nnn.nn
Diffstat:
M | sbm.c | | | 27 | +++++++++++++++------------ |
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/sbm.c b/sbm.c
@@ -139,22 +139,24 @@ sample(char *ifname, uint64_t *rxbytes, uint64_t *txbytes,
}
#endif
-void
-scale(char **suffix, uint64_t *bits)
+double
+scale(char **suffix, uint64_t bits)
{
static char *suffixes[][3] = { { "bps", "Kbps", "Mbps" },
{ "Bps", "KBps", "MBps" } };
+ double rounded = bits;
if (Bflag)
- *bits /= 8;
+ rounded /= 8;
*suffix = suffixes[Bflag][0];
- if (*bits >= 1000 && *bits < 1000 * 1000) {
+ if (rounded >= 1000 && rounded < 1000 * 1000) {
*suffix = suffixes[Bflag][1];
- *bits /= 1000;
- } else if (*bits >= 1000 * 1000) {
+ rounded /= 1000;
+ } else if (rounded >= 1000 * 1000) {
*suffix = suffixes[Bflag][2];
- *bits /= 1000 * 1000;
+ rounded /= 1000 * 1000;
}
+ return rounded;
}
void
@@ -162,15 +164,16 @@ print(char *ifname, uint64_t rxbits, uint64_t txbits,
uint64_t rxpps, uint64_t txpps)
{
char *rxsuffix, *txsuffix;
+ double rxround, txround;
if (!tflag) {
- scale(&rxsuffix, &rxbits);
- scale(&txsuffix, &txbits);
- printf("%s: %3llu Rx %4s %3llu Tx %4s %5llu Rx pps %5llu Tx pps\n",
+ rxround = scale(&rxsuffix, rxbits);
+ txround = scale(&txsuffix, txbits);
+ printf("%s: %6.2f Rx %4s %6.2f Tx %4s %5llu Rx pps %5llu Tx pps\n",
ifname,
- (unsigned long long)rxbits,
+ rxround,
rxsuffix,
- (unsigned long long)txbits,
+ txround,
txsuffix,
(unsigned long long)rxpps,
(unsigned long long)txpps);