net: increase speedrate precision (#2054)

more granular units need lower precision, while less granular need
higher precision. assume sane default of:

unit | precision
KB   | 0
MB   | 1
GB   | 2

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
This commit is contained in:
Jan Palus 2020-12-01 17:54:54 +01:00 committed by GitHub
parent 26a9bc5964
commit 9f2459be8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -229,15 +229,17 @@ namespace net {
float time_diff = duration.count();
float speedrate = bytes_diff / time_diff;
vector<string> suffixes{"G", "M"};
vector<pair<string,int>> units{make_pair("G", 2), make_pair("M", 1)};
string suffix{"K"};
int precision = 0;
while ((speedrate /= 1000) > 999) {
suffix = suffixes.back();
suffixes.pop_back();
suffix = units.back().first;
precision = units.back().second;
units.pop_back();
}
return sstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(0) << std::fixed << speedrate
return sstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(precision) << std::fixed << speedrate
<< " " << suffix << unit;
}