Skip to content

Commit 6691328

Browse files
Nicolas Pitregitster
authored andcommitted
progress bar: round to the nearest instead of truncating down
Often the throughput output is requested when the data read so far is one smaller than multiple of 1024; because 1023/1024 is ~0.999, it often shows up as 0.99 because the code currently truncates. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f29ac4f commit 6691328

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

progress.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ static void throughput_string(struct throughput *tp, off_t total,
121121
(int)(total >> 30),
122122
(int)(total & ((1 << 30) - 1)) / 10737419);
123123
} else if (total > 1 << 20) {
124+
int x = total + 5243; /* for rounding */
124125
l -= snprintf(tp->display, l, ", %u.%2.2u MiB",
125-
(int)(total >> 20),
126-
((int)(total & ((1 << 20) - 1)) * 100) >> 20);
126+
x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
127127
} else if (total > 1 << 10) {
128+
int x = total + 5; /* for rounding */
128129
l -= snprintf(tp->display, l, ", %u.%2.2u KiB",
129-
(int)(total >> 10),
130-
((int)(total & ((1 << 10) - 1)) * 100) >> 10);
130+
x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
131131
} else {
132132
l -= snprintf(tp->display, l, ", %u bytes", (int)total);
133133
}

0 commit comments

Comments
 (0)