Skip to content

Conversation

@adienakhmad
Copy link
Contributor

A fix for Utf8Formatter mistakenly removing zeros when formatting a floating-point whole number.

The existing logic would format floating-point value of 10 as "1", removing the zero.
To fix this, the removal of trailing zeros and the decimal separator should be handled separately.

…g-point whole number

The prev logic would format floating-point value of 10 as "1".
To fix this, the removal of trailing zeros and the decimal separator should be handled separately.
@JunaMeinhold
Copy link
Member

Hi, thanks for your pull requests and your work,

The code looks good overall, but I would suggest one change. Instead of checking the separator character directly, it’s safer to store the buffer pointer before writing the separator and later compare the buffer pointer to that stored value. This approach is faster and avoids issues with multi-byte UTF-8 characters, such as some full-width punctuation used in Chinese or other locales.

Full-width period 。 (U+3002)
Full-width comma , (U+FF0C)
Other full-width numeric punctuation
These characters are multi-byte in UTF-8 (e.g., , is three bytes: 0xEF 0xBC 0x8C).

byte* beforeSeparator = buffer; 
buffer += ConvertUtf16ToUtf8(format.CurrencyDecimalSeparator, buffer, (int)(end - buffer)); 
byte* afterSeparator = buffer; 

// ... write fraction digits ...

while (buffer != afterSeparator && *(buffer - 1) == '0') { buffer--; } 

if (buffer == afterSeparator) { buffer = beforeSeparator; }

@adienakhmad
Copy link
Contributor Author

I've updated the code as suggested. I've updated the test as well to use a custom culture as none of the cultures listed in CultureInfo.GetCultures(CultureTypes.AllCultures) uses 3-bytes UTF-8 character as the separator.

@JunaMeinhold JunaMeinhold merged commit b3a3a3e into HexaEngine:master Aug 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants