diff options
Diffstat (limited to 'src/zencore/string.cpp')
| -rw-r--r-- | src/zencore/string.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/zencore/string.cpp b/src/zencore/string.cpp index 358722b0b..df7d71250 100644 --- a/src/zencore/string.cpp +++ b/src/zencore/string.cpp @@ -381,6 +381,34 @@ NiceNumGeneral(uint64_t Num, std::span<char> Buffer, NicenumFormat Format) } size_t +ThousandsToBuffer(uint64_t Num, std::span<char> Buffer) +{ + // Format into a temporary buffer without separators + char Tmp[24]; + int Len = snprintf(Tmp, sizeof(Tmp), "%llu", (unsigned long long)Num); + + // Insert comma separators + int SepCount = (Len - 1) / 3; + int TotalLen = Len + SepCount; + ZEN_ASSERT(TotalLen < (int)Buffer.size()); + + int Src = Len - 1; + int Dst = TotalLen; + Buffer[Dst--] = '\0'; + + for (int i = 0; Src >= 0; i++) + { + if (i > 0 && i % 3 == 0) + { + Buffer[Dst--] = ','; + } + Buffer[Dst--] = Tmp[Src--]; + } + + return TotalLen; +} + +size_t NiceNumToBuffer(uint64_t Num, std::span<char> Buffer) { return NiceNumGeneral(Num, Buffer, kNicenum1024); |