Skip to content

Commit

Permalink
formats: reduce 1 digits in si prefix
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Nov 23, 2024
1 parent 5bc4feb commit 41a1ee0
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions contrib/csnippets/utils/formats.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ int format_si_prefix(char *buf, const size_t bufsize, const double value)
}
const int e = (int)floor(log10(fabs(value)) / 3.0);
if (e == 0) {
return snprintf(buf, bufsize, "%.4g", value);
return snprintf(buf, bufsize, "%.3g", value);
}
if (e < 0) {
const size_t i = MIN((size_t)-e, ARRAY_SIZE(si_prefix_neg));
const double v = value / pow(10, -3.0 * (double)i);
const char *prefix = si_prefix_neg[i - 1];
return snprintf(buf, bufsize, "%.4g%s", v, prefix);
return snprintf(buf, bufsize, "%.3g%s", v, prefix);
}
const size_t i = MIN((size_t)e, ARRAY_SIZE(si_prefix_pos));
const double v = value / pow(10, 3.0 * (double)i);
const char *prefix = si_prefix_pos[i - 1];
return snprintf(buf, bufsize, "%.4g%s", v, prefix);
return snprintf(buf, bufsize, "%.3g%s", v, prefix);
}

static const char *iec_units[] = {
Expand Down

0 comments on commit 41a1ee0

Please sign in to comment.