mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 00:37:23 +01:00
FFstrbuf: handles double rounding and zero representation in formatter
This commit is contained in:
+5
-1
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <math.h>
|
||||
|
||||
char* CHAR_NULL_PTR = "";
|
||||
|
||||
@@ -577,9 +578,12 @@ void ffStrbufAppendDouble(FFstrbuf* strbuf, double value, int8_t precision)
|
||||
ffStrbufEnsureFree(strbuf, 40); // Required by yyjson_write_number
|
||||
char* start = strbuf->chars + strbuf->length;
|
||||
|
||||
if (precision == 0)
|
||||
value = round(value);
|
||||
yyjson_val val = {};
|
||||
unsafe_yyjson_set_double(&val, value);
|
||||
if (precision >= 0) unsafe_yyjson_set_fp_to_fixed(&val, precision == 0 ? 1 : precision); // yyjson ignores precision == 0
|
||||
if (precision > 0)
|
||||
unsafe_yyjson_set_fp_to_fixed(&val, precision);
|
||||
|
||||
// Write at most <precision> digits after the decimal point; doesn't append trailing zeros
|
||||
char* end = yyjson_write_number(&val, start);
|
||||
|
||||
+19
-3
@@ -759,6 +759,14 @@ int main(void)
|
||||
ffStrbufAppendDouble(&strbuf, 120.123456789, 5);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "120.12346"));
|
||||
|
||||
ffStrbufClear(&strbuf);
|
||||
ffStrbufAppendDouble(&strbuf, 120.888888, 0);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "121"));
|
||||
|
||||
ffStrbufClear(&strbuf);
|
||||
ffStrbufAppendDouble(&strbuf, 120.999999, 2);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "121.00"));
|
||||
|
||||
ffStrbufClear(&strbuf);
|
||||
ffStrbufAppendDouble(&strbuf, 120.123456789, 0);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "120"));
|
||||
@@ -799,6 +807,14 @@ int main(void)
|
||||
ffStrbufAppendDouble(&strbuf, -120.123, 5);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "-120.12300"));
|
||||
|
||||
ffStrbufClear(&strbuf);
|
||||
ffStrbufAppendDouble(&strbuf, -120.888888, 0);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "-121"));
|
||||
|
||||
ffStrbufClear(&strbuf);
|
||||
ffStrbufAppendDouble(&strbuf, -120.999999, 2);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "-121.00"));
|
||||
|
||||
ffStrbufClear(&strbuf);
|
||||
ffStrbufAppendDouble(&strbuf, 1.2345e50, 1);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "1.2345e50"));
|
||||
@@ -832,12 +848,12 @@ int main(void)
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "-123450000000000000000.0"));
|
||||
|
||||
ffStrbufClear(&strbuf);
|
||||
ffStrbufAppendDouble(&strbuf, 0, 0);
|
||||
ffStrbufAppendDouble(&strbuf, +0.0, 0);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "0"));
|
||||
|
||||
ffStrbufClear(&strbuf);
|
||||
ffStrbufAppendDouble(&strbuf, -0, 0);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "0"));
|
||||
ffStrbufAppendDouble(&strbuf, -0.0, 0);
|
||||
VERIFY(ffStrbufEqualS(&strbuf, "-0"));
|
||||
|
||||
ffStrbufDestroy(&strbuf);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user