btop icon indicating copy to clipboard operation
btop copied to clipboard

[BUG] Incorrect conversion of swap memory size when resizing window

Open matteoguglielmi opened this issue 1 month ago • 2 comments

Describe the bug

Incorrect conversion of swap memory size when resizing window.

btop version is 1.4.5

To Reproduce

I have 10 GiB of swap memory.

Btop says I have "9.99 GiB".

When I make the window small enough to trigger the memory conversion (no spaces, fewer digits/characters), btop says I have "1.0T".

Expected behavior

Btop says I have "10G"

Screenshots

Correct:

Image

Wrong:

Image

matteoguglielmi avatar Nov 22 '25 12:11 matteoguglielmi

I am unable to reproduce this on 24.04

Image Image

AbhinavGor avatar Nov 25 '25 18:11 AbhinavGor

Seems like a 9.99 specific issue. The offending code seems to be this section in Tools:floating_humanizer in btop_tools.cpp:

// out = "9.99" here, 4 chars long
if (shorten) {
	auto f_pos = out.find(".");
	if (f_pos == 1 and out.size() > 3) {         // expects to shorten out by one character by rounding it to one decimal place
		out = fmt::format("{:.1f}", stod(out)); // but 9.99 rounds up to 10.0 so it stays a 4 chars long
	}
	else if (f_pos != string::npos) {
		out = fmt::format("{:.0f}", stod(out));
	}
	if (out.size() > 3) {                        // hence this part truncates out to just the first digit and bumps the unit
		out = fmt::format("{:d}.0", out[0] - '0');
		start++;
	}
	out.push_back(units[start][0]);
}
else out += " " + units[start];

yinghao-w avatar Nov 26 '25 02:11 yinghao-w