]> git.ipfire.org Git - thirdparty/haproxy.git/commit
MINOR: tools: implement trimming of floating point numbers
authorWilly Tarreau <w@1wt.eu>
Sat, 8 May 2021 08:28:53 +0000 (10:28 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 8 May 2021 08:42:11 +0000 (10:42 +0200)
commit56d1d8dab0aed7f781486a34f1f47de4d0c3648f
treeece20078509dd48b70b3026d5c6550fc5af5c466
parent589722ed2c4e206c9378a546d87be00e53261c8c
MINOR: tools: implement trimming of floating point numbers

When using "%f" to print a float, it automatically gets 6 digits after
the decimal point and there's no way to automatically adjust to the
required ones by dropping trailing zeroes. This function does exactly
this and automatically drops the decimal point if all digits after it
were zeroes. This will make numbers more friendly in stats and makes
outputs shorter (e.g. JSON where everything is just a "number").

The function is designed to be easy to use with snprint() and chunks:

  snprintf:
    flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", x));

  chunk_printf:
    out->data = flt_trim(out->area, 0, chunk_printf(out, "%f", x));

  chunk_appendf:
    size_t prev_data = out->data;
    out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", x));
include/haproxy/tools.h
src/tools.c