From 82e703b6ad3836c9c8c4070aec27308b6601e49b Mon Sep 17 00:00:00 2001 From: Chloe <25387744+qimiko@users.noreply.github.com> Date: Sun, 13 Oct 2024 06:13:19 -0700 Subject: [PATCH] truncate numbers in numtoabbreviatedstring --- loader/include/Geode/utils/general.hpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/loader/include/Geode/utils/general.hpp b/loader/include/Geode/utils/general.hpp index 60b6e767..73b64137 100644 --- a/loader/include/Geode/utils/general.hpp +++ b/loader/include/Geode/utils/general.hpp @@ -99,9 +99,22 @@ namespace geode { */ template 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(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(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); }