ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
format.h File Reference

📊 String Formatting Utilities More...

Go to the source code of this file.

Functions

void format_bytes_pretty (size_t bytes, char *out, size_t out_capacity)
 Format byte count into human-readable string.
void format_number_compact (uint64_t value, char *out, size_t out_capacity)

Detailed Description

📊 String Formatting Utilities

Definition in file util/format.h.

Function Documentation

◆ format_number_compact()

void format_number_compact ( uint64_t value,
char * out,
size_t out_capacity )

Format a decimal count with lowercase k/m/b/t/p/e suffixes. Uses at most one rounded decimal place, omitting a trailing .0. Values below 1,000 stay exact; rounded 1,000-unit values promote a tier. Output is NUL-terminated when capacity is nonzero; NULL output is ignored.

Definition at line 53 of file util/format.c.

53 {
54 if (!out || !out_capacity)
55 return;
56 static const char suffixes[] = "kmbtpe";
57 if (value < 1000) {
58 SAFE_IGNORE_PRINTF_RESULT(
59 safe_snprintf(out, out_capacity, "%" PRIu64, value));
60 return;
61 }
62 uint64_t scale = 1000;
63 size_t unit = 0;
64 while (unit + 1 < sizeof(suffixes) - 1 && value / scale >= 1000) {
65 scale *= 1000;
66 ++unit;
67 }
68 uint64_t whole = value / scale;
69 uint64_t step = scale / 10;
70 uint64_t fraction = (value % scale + step / 2) / step;
71 if (fraction == 10) {
72 ++whole;
73 fraction = 0;
74 }
75 if (whole == 1000 && unit + 1 < sizeof(suffixes) - 1) {
76 whole = 1;
77 ++unit;
78 }
79 if (fraction)
80 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity,
81 "%" PRIu64 ".%" PRIu64 "%c", whole,
82 fraction, suffixes[unit]));
83 else
84 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%" PRIu64 "%c",
85 whole, suffixes[unit]));
86}
unsigned long long uint64_t
static bool size_t gotel_query_t * out
Definition queries.h:10

References out.

Referenced by ui_sync_render().