Skip to content

Commit 54d8b0a

Browse files
committed
Global: adds configurable trailing zeros for double formatting
1 parent 48fc72e commit 54d8b0a

File tree

8 files changed

+196
-43
lines changed

8 files changed

+196
-43
lines changed

src/common/format.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ void ffFormatAppendFormatArg(FFstrbuf* buffer, const FFformatarg* formatarg)
3232
ffStrbufAppend(buffer, (const FFstrbuf*)formatarg->value);
3333
break;
3434
case FF_FORMAT_ARG_TYPE_FLOAT:
35-
ffStrbufAppendDouble(buffer, *(float*)formatarg->value, instance.config.display.fractionNdigits);
35+
ffStrbufAppendDouble(buffer, *(float*)formatarg->value, instance.config.display.fractionNdigits, instance.config.display.fractionTrailingZeros != FF_FRACTION_TRAILING_ZEROS_TYPE_HIDE);
3636
break;
3737
case FF_FORMAT_ARG_TYPE_DOUBLE:
38-
ffStrbufAppendDouble(buffer, *(double*)formatarg->value, instance.config.display.fractionNdigits);
38+
ffStrbufAppendDouble(buffer, *(double*)formatarg->value, instance.config.display.fractionNdigits, instance.config.display.fractionTrailingZeros != FF_FRACTION_TRAILING_ZEROS_TYPE_HIDE);
3939
break;
4040
case FF_FORMAT_ARG_TYPE_BOOL:
4141
ffStrbufAppendS(buffer, *(bool*)formatarg->value ? "true" : "false");

src/common/frequency.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ bool ffFreqAppendNum(uint32_t mhz, FFstrbuf* result)
1111

1212
if (ndigits >= 0)
1313
{
14-
ffStrbufAppendDouble(result, mhz / 1000., ndigits);
14+
ffStrbufAppendDouble(result, mhz / 1000., ndigits, true);
1515
if (spaceBeforeUnit) ffStrbufAppendC(result, ' ');
1616
ffStrbufAppendS(result, "GHz");
1717
}

src/common/size.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static void appendNum(FFstrbuf* result, uint64_t bytes, uint32_t base, const cha
1717
if (counter == 0)
1818
ffStrbufAppendUInt(result, bytes);
1919
else
20-
ffStrbufAppendDouble(result, size, (int8_t) options->sizeNdigits);
20+
ffStrbufAppendDouble(result, size, (int8_t) options->sizeNdigits, true);
2121
if (options->sizeSpaceBeforeUnit != FF_SPACE_BEFORE_UNIT_NEVER)
2222
ffStrbufAppendC(result, ' ');
2323
ffStrbufAppendS(result, prefixes[counter]);

src/options/display.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,24 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
380380
return "display.fraction.ndigits must be an integer";
381381
options->fractionNdigits = (int8_t) yyjson_get_int(ndigits);
382382
}
383+
yyjson_val* trailingZeros = yyjson_obj_get(val, "trailingZeros");
384+
if (trailingZeros)
385+
{
386+
if (yyjson_is_null(trailingZeros))
387+
options->fractionTrailingZeros = FF_FRACTION_TRAILING_ZEROS_TYPE_DEFAULT;
388+
else
389+
{
390+
int value;
391+
const char* error = ffJsonConfigParseEnum(trailingZeros, &value, (FFKeyValuePair[]) {
392+
{ "default", FF_FRACTION_TRAILING_ZEROS_TYPE_DEFAULT },
393+
{ "show", FF_FRACTION_TRAILING_ZEROS_TYPE_SHOW },
394+
{ "hide", FF_FRACTION_TRAILING_ZEROS_TYPE_HIDE },
395+
{},
396+
});
397+
if (error) return error;
398+
options->fractionTrailingZeros = (FFFractionTrailingZerosType) value;
399+
}
400+
}
383401
}
384402
else
385403
return "display.fraction must be an object";
@@ -810,6 +828,7 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options)
810828
options->freqNdigits = 2;
811829
options->freqSpaceBeforeUnit = FF_SPACE_BEFORE_UNIT_DEFAULT;
812830
options->fractionNdigits = -1;
831+
options->fractionTrailingZeros = FF_FRACTION_TRAILING_ZEROS_TYPE_DEFAULT;
813832

814833
ffListInit(&options->constants, sizeof(FFstrbuf));
815834
}

src/options/display.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ typedef enum __attribute__((__packed__)) FFSpaceBeforeUnitType
2525
FF_SPACE_BEFORE_UNIT_NEVER,
2626
} FFSpaceBeforeUnitType;
2727

28+
typedef enum __attribute__((__packed__)) FFFractionTrailingZerosType
29+
{
30+
FF_FRACTION_TRAILING_ZEROS_TYPE_DEFAULT,
31+
FF_FRACTION_TRAILING_ZEROS_TYPE_SHOW,
32+
FF_FRACTION_TRAILING_ZEROS_TYPE_HIDE,
33+
} FFFractionTrailingZerosType;
34+
2835
typedef struct FFOptionsDisplay
2936
{
3037
//If one of those is empty, ffLogoPrint will set them
@@ -81,6 +88,7 @@ typedef struct FFOptionsDisplay
8188
int8_t freqNdigits;
8289
FFSpaceBeforeUnitType freqSpaceBeforeUnit;
8390
int8_t fractionNdigits;
91+
FFFractionTrailingZerosType fractionTrailingZeros;
8492

8593
FFlist constants; // list of FFstrbuf
8694
} FFOptionsDisplay;

src/util/FFstrbuf.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ void ffStrbufAppendUInt(FFstrbuf* strbuf, uint64_t value)
571571
strbuf->length += (uint32_t)(end - start);
572572
}
573573

574-
void ffStrbufAppendDouble(FFstrbuf* strbuf, double value, int8_t precision)
574+
void ffStrbufAppendDouble(FFstrbuf* strbuf, double value, int8_t precision, bool trailingZeros)
575575
{
576576
assert(precision <= 15); // yyjson_write_number supports up to 15 digits after the decimal point
577577

@@ -598,18 +598,29 @@ void ffStrbufAppendDouble(FFstrbuf* strbuf, double value, int8_t precision)
598598
return;
599599
}
600600

601-
if (precision > 1)
601+
if (trailingZeros)
602602
{
603-
for (char* p = end - 1; *p != '.' && p > start; --p)
604-
--precision;
605-
if (precision > 0)
606-
ffStrbufAppendNC(strbuf, (uint32_t) precision, '0');
603+
if (precision > 1)
604+
{
605+
for (char* p = end - 1; *p != '.' && p > start; --p)
606+
--precision;
607+
if (precision > 0)
608+
ffStrbufAppendNC(strbuf, (uint32_t) precision, '0');
609+
}
610+
else if (precision == 0 || (precision < 0 && end[-1] == '0'))
611+
{
612+
goto removeDecimalPoint;
613+
}
607614
}
608-
else if (precision == 0 || (precision < 0 && end[-1] == '0'))
615+
else
609616
{
610-
// yyjson always appends ".0", so we need to remove it
611-
strbuf->length -= 2;
612-
strbuf->chars[strbuf->length] = '\0';
617+
if (end[-1] == '0')
618+
{
619+
removeDecimalPoint:
620+
// yyjson always appends ".0" to make it a float point number. We need to remove it
621+
strbuf->length -= 2;
622+
strbuf->chars[strbuf->length] = '\0';
623+
}
613624
}
614625
}
615626

src/util/FFstrbuf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void ffStrbufAppendSInt(FFstrbuf* strbuf, int64_t value);
107107
void ffStrbufAppendUInt(FFstrbuf* strbuf, uint64_t value);
108108
// Appends a double value to the string buffer with the specified precision (0~15).
109109
// if `precision < 0`, let yyjson decide the precision
110-
void ffStrbufAppendDouble(FFstrbuf* strbuf, double value, int8_t precision);
110+
void ffStrbufAppendDouble(FFstrbuf* strbuf, double value, int8_t precision, bool trailingZeros);
111111

112112
FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateA(uint32_t allocate)
113113
{

0 commit comments

Comments
 (0)