diff options
author | Hop311 <Hop3114@gmail.com> | 2024-05-09 23:32:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-09 23:32:18 +0200 |
commit | b0a533f945bbc6201fd7df4bc60746cb98efaba4 (patch) | |
tree | 56c84e804b11a271e15f38e64d5b3727c636b474 /extension/src/openvic-extension/utility | |
parent | f57bbe6604a237c9fea52aec43641585d0b24568 (diff) | |
parent | a3ef8c1ad72f7e839e073679f374195681208837 (diff) |
Merge pull request #228 from OpenVicProject/menu-tweaks
Topbar display data + Population menu rebel icons
Diffstat (limited to 'extension/src/openvic-extension/utility')
-rw-r--r-- | extension/src/openvic-extension/utility/Utilities.cpp | 26 | ||||
-rw-r--r-- | extension/src/openvic-extension/utility/Utilities.hpp | 8 |
2 files changed, 29 insertions, 5 deletions
diff --git a/extension/src/openvic-extension/utility/Utilities.cpp b/extension/src/openvic-extension/utility/Utilities.cpp index 4389e95..694b658 100644 --- a/extension/src/openvic-extension/utility/Utilities.cpp +++ b/extension/src/openvic-extension/utility/Utilities.cpp @@ -13,7 +13,7 @@ using namespace OpenVic; /* Int to 2 decimal place string in terms of the largest suffix less than or equal to it, * or normal integer string if less than the smallest suffix. */ -String Utilities::int_to_formatted_string(int64_t val) { +String Utilities::int_to_string_suffixed(int64_t val) { static const std::vector<std::pair<int64_t, String>> suffixes { { 1'000'000'000'000, "T" }, { 1'000'000'000, "B" }, @@ -36,8 +36,30 @@ String Utilities::int_to_formatted_string(int64_t val) { return (negative ? "-" : "") + String::num_int64(val); } +String Utilities::float_to_string_suffixed(float val) { + const float abs_val = std::abs(val); + + if (abs_val < 10'000.0f) { + return float_to_string_dp(val, 1); + } + + if (abs_val < 1'000'000.0f) { + return float_to_string_dp(val / 1'000.0f, 2) + "k"; + } + + if (abs_val < 1'000'000'000.0f) { + return float_to_string_dp(val / 1'000'000.0f, 2) + "M"; + } + + if (abs_val < 1'000'000'000'000.0f) { + return float_to_string_dp(val / 1'000'000'000.0f, 2) + "B"; + } + + return float_to_string_dp(val / 1'000'000'000'000.0f, 2) + "T"; +} + /* Float to string formatted with the specified number of decimal places. */ -String Utilities::float_to_formatted_string(float val, int32_t decimal_places) { +String Utilities::float_to_string_dp(float val, int32_t decimal_places) { return String::num(val, decimal_places).pad_decimals(decimal_places); } diff --git a/extension/src/openvic-extension/utility/Utilities.hpp b/extension/src/openvic-extension/utility/Utilities.hpp index 15ff6b6..0cd9edc 100644 --- a/extension/src/openvic-extension/utility/Utilities.hpp +++ b/extension/src/openvic-extension/utility/Utilities.hpp @@ -33,9 +33,11 @@ namespace OpenVic::Utilities { return std_to_godot_string_name(static_cast<std::string>(str)); } - godot::String int_to_formatted_string(int64_t val); + godot::String int_to_string_suffixed(int64_t val); - godot::String float_to_formatted_string(float val, int32_t decimal_places); + godot::String float_to_string_suffixed(float val); + + godot::String float_to_string_dp(float val, int32_t decimal_places); constexpr real_t to_real_t(std::floating_point auto val) { return static_cast<real_t>(val); @@ -83,4 +85,4 @@ namespace OpenVic::Utilities { namespace literals { constexpr real_t operator""_real(long double val) { return to_real_t(val); } } -}
\ No newline at end of file +} |