mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-14 19:15:05 -05:00
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
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:
parent
5f7008072e
commit
82e703b6ad
1 changed files with 16 additions and 3 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue