truncate numbers in numtoabbreviatedstring
Some checks are pending
Build Binaries / Build Windows (push) Waiting to run
Build Binaries / Build macOS (push) Waiting to run
Build Binaries / Build Android (64-bit) (push) Waiting to run
Build Binaries / Build Android (32-bit) (push) Waiting to run
Build Binaries / Publish (push) Blocked by required conditions

This commit is contained in:
Chloe 2024-10-13 06:13:19 -07:00
parent 5f7008072e
commit 82e703b6ad
No known key found for this signature in database
GPG key ID: D2D404DD810FE0E3

View file

@ -99,9 +99,22 @@ namespace geode {
*/
template <std::integral Num>
std::string numToAbbreviatedString(Num num) {
if (num >= 1'000'000'000) return fmt::format("{:0.3}B", num / 1'000'000'000.f);
if (num >= 1'000'000) return fmt::format("{:0.3}M", num / 1'000'000.f);
if (num >= 1'000) return fmt::format("{:0.3}K", num / 1'000.f);
// it's a mess... i'm sorry...
constexpr auto numToFixedTrunc = [](float num) {
// calculate the number of digits we keep from the decimal
auto remaining = std::max(3 - static_cast<int>(std::log10(num)) - 1, 0);
auto factor = std::pow(10, remaining);
auto trunc = std::trunc(num * factor) / factor;
// doing this dynamic format thing lets the .0 show when needed
return fmt::format("{:0.{}f}", trunc, static_cast<int>(remaining));
};
if (num >= 1'000'000'000) return fmt::format("{}B", numToFixedTrunc(num / 1'000'000'000.f));
if (num >= 1'000'000) return fmt::format("{}M", numToFixedTrunc(num / 1'000'000.f));
if (num >= 1'000) return fmt::format("{}K", numToFixedTrunc(num / 1'000.f));
return numToString(num);
}