bytes_to_human_str support round off

use_iouring
YuQing 2025-09-03 15:23:27 +08:00
parent 84a1f90a9a
commit b97f23ced2
1 changed files with 23 additions and 4 deletions

View File

@ -3254,17 +3254,36 @@ const char *long2str(const int64_t n, char *buff, const bool thousands_separator
static int format_bytes_string(const int64_t input_bytes, static int format_bytes_string(const int64_t input_bytes,
const int64_t unit_bytes, char *buff) const int64_t unit_bytes, char *buff)
{ {
int64_t remain;
int n; int n;
int fragment; int fragment;
char *p; char *p;
n = input_bytes / unit_bytes; n = input_bytes / unit_bytes;
p = buff + fc_itoa(n, buff); remain = input_bytes - (n * unit_bytes);
if (n < 10) if (n < 10)
{ {
fragment = ((input_bytes - (n * unit_bytes)) * 10LL) / unit_bytes; fragment = (remain * 10LL + unit_bytes / 2) / unit_bytes;
*p++ = '.'; if (fragment == 10)
p += fc_itoa(fragment, p); {
++n;
fragment = 0;
}
p = buff + fc_itoa(n, buff);
if (n < 10)
{
*p++ = '.';
p += fc_itoa(fragment, p);
}
}
else
{
if (remain >= unit_bytes / 2)
{
++n;
}
p = buff + fc_itoa(n, buff);
} }
return p - buff; return p - buff;
} }