aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/utility
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-12-11 12:08:55 +0100
committer GitHub <noreply@github.com>2023-12-11 12:08:55 +0100
commit0b3a162d3db840996c3d89e65dfe9270faebffe5 (patch)
treeb22d464dbf8e0e2569b9be5aa130e4def2e51207 /extension/src/openvic-extension/utility
parenta6952efba078e49d6555b0586230986a2cb7ed40 (diff)
parentfd375bdb35d8a7b2ac9cf3dd02cdb0f197451a0b (diff)
Merge pull request #169 from OpenVicProject/guinode
Big UI commit - GUINode, MaskedFlag, PieChart, etc
Diffstat (limited to 'extension/src/openvic-extension/utility')
-rw-r--r--extension/src/openvic-extension/utility/Utilities.cpp31
-rw-r--r--extension/src/openvic-extension/utility/Utilities.hpp4
2 files changed, 35 insertions, 0 deletions
diff --git a/extension/src/openvic-extension/utility/Utilities.cpp b/extension/src/openvic-extension/utility/Utilities.cpp
index e3bcce6..099b5a9 100644
--- a/extension/src/openvic-extension/utility/Utilities.cpp
+++ b/extension/src/openvic-extension/utility/Utilities.cpp
@@ -13,6 +13,37 @@
using namespace godot;
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) {
+ static const std::vector<std::pair<int64_t, String>> suffixes {
+ { 1'000'000'000'000, "T" },
+ { 1'000'000'000, "B" },
+ { 1'000'000, "M" },
+ { 1'000, "k" }
+ };
+ static constexpr int64_t decimal_places_multiplier = 100;
+ const bool negative = val < 0;
+ if (negative) {
+ val = -val;
+ }
+ for (auto const& [suffix_val, suffix_str] : suffixes) {
+ if (val >= suffix_val) {
+ const int64_t whole = val / suffix_val;
+ const int64_t frac = (val * decimal_places_multiplier / suffix_val) % decimal_places_multiplier;
+ return (negative ? "-" : "") + String::num_int64(whole) + "." +
+ (frac < 10 ? "0" : "") + String::num_int64(frac) + suffix_str;
+ }
+ }
+ return (negative ? "-" : "") + String::num_int64(val);
+}
+
+/* Float to formatted to 4 decimal place string. */
+String Utilities::float_to_formatted_string(float val) {
+ static constexpr int64_t decimal_places = 4;
+ return String::num(val, decimal_places).pad_decimals(decimal_places);
+}
+
/* Date formatted like this: "January 1, 1836" (with the month localised, if possible). */
String Utilities::date_to_formatted_string(Date date) {
std::string const& month_name = date.get_month_name();
diff --git a/extension/src/openvic-extension/utility/Utilities.hpp b/extension/src/openvic-extension/utility/Utilities.hpp
index 9537dda..6eeb285 100644
--- a/extension/src/openvic-extension/utility/Utilities.hpp
+++ b/extension/src/openvic-extension/utility/Utilities.hpp
@@ -31,6 +31,10 @@ 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 float_to_formatted_string(float val);
+
godot::String date_to_formatted_string(Date date);
inline godot::Color to_godot_color(colour_t colour) {