diff options
author | kobewi <kobewi4e@gmail.com> | 2024-03-26 17:04:14 +0100 |
---|---|---|
committer | kobewi <kobewi4e@gmail.com> | 2024-10-23 15:00:21 +0200 |
commit | 5c0f2414cd6d988a4eab93fc21bc3b80388907e8 (patch) | |
tree | 05d11ea1b6d97b2cb8e2e203dba1ffb89dc70eb5 /core | |
parent | 533c616cb86ff7bb940d58ffbbcc1a3eca0aa33d (diff) | |
download | redot-engine-5c0f2414cd6d988a4eab93fc21bc3b80388907e8.tar.gz |
Always add decimal when printing float
Diffstat (limited to 'core')
-rw-r--r-- | core/extension/extension_api_dump.cpp | 6 | ||||
-rw-r--r-- | core/math/rect2.cpp | 2 | ||||
-rw-r--r-- | core/math/vector2.cpp | 2 | ||||
-rw-r--r-- | core/math/vector3.cpp | 2 | ||||
-rw-r--r-- | core/string/ustring.cpp | 28 | ||||
-rw-r--r-- | core/string/ustring.h | 1 | ||||
-rw-r--r-- | core/variant/variant.cpp | 2 |
7 files changed, 32 insertions, 11 deletions
diff --git a/core/extension/extension_api_dump.cpp b/core/extension/extension_api_dump.cpp index c5f7502c12..70785517a8 100644 --- a/core/extension/extension_api_dump.cpp +++ b/core/extension/extension_api_dump.cpp @@ -1371,6 +1371,9 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_ Dictionary elem = var; ERR_FAIL_COND_V_MSG(!elem.has(p_name_field), false, vformat("Validate extension JSON: Element of base_array '%s' is missing field '%s'. This is a bug.", base_array, p_name_field)); String name = elem[p_name_field]; + if (name.is_valid_float()) { + name = name.trim_suffix(".0"); // Make "integers" stringified as integers. + } if (p_compare_operators && elem.has("right_type")) { name += " " + String(elem["right_type"]); } @@ -1386,6 +1389,9 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_ continue; } String name = old_elem[p_name_field]; + if (name.is_valid_float()) { + name = name.trim_suffix(".0"); // Make "integers" stringified as integers. + } if (p_compare_operators && old_elem.has("right_type")) { name += " " + String(old_elem["right_type"]); } diff --git a/core/math/rect2.cpp b/core/math/rect2.cpp index c55226a57e..7f77b0786c 100644 --- a/core/math/rect2.cpp +++ b/core/math/rect2.cpp @@ -283,7 +283,7 @@ next4: } Rect2::operator String() const { - return "[P: " + position.operator String() + ", S: " + size + "]"; + return "[P: " + position.operator String() + ", S: " + size.operator String() + "]"; } Rect2::operator Rect2i() const { diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index e86b97d6a8..0590ee8a37 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -203,7 +203,7 @@ bool Vector2::is_finite() const { } Vector2::operator String() const { - return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ")"; + return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ")"; } Vector2::operator Vector2i() const { diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index 1e90002665..e18ac3b011 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -165,7 +165,7 @@ bool Vector3::is_finite() const { } Vector3::operator String() const { - return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ")"; + return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ", " + String::num_real(z, true) + ")"; } Vector3::operator Vector3i() const { diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index e6f7492a18..ef72ac7fff 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -1818,7 +1818,7 @@ String String::num(double p_num, int p_decimals) { #endif buf[324] = 0; - //destroy trailing zeroes + // Destroy trailing zeroes, except one after period. { bool period = false; int z = 0; @@ -1835,7 +1835,7 @@ String String::num(double p_num, int p_decimals) { if (buf[z] == '0') { buf[z] = 0; } else if (buf[z] == '.') { - buf[z] = 0; + buf[z + 1] = '0'; break; } else { break; @@ -1924,14 +1924,28 @@ String String::num_real(double p_num, bool p_trailing) { return num_int64((int64_t)p_num); } } -#ifdef REAL_T_IS_DOUBLE int decimals = 14; -#else + // We want to align the digits to the above sane default, so we only need + // to subtract log10 for numbers with a positive power of ten magnitude. + const double abs_num = Math::abs(p_num); + if (abs_num > 10) { + decimals -= (int)floor(log10(abs_num)); + } + return num(p_num, decimals); +} + +String String::num_real(float p_num, bool p_trailing) { + if (p_num == (float)(int64_t)p_num) { + if (p_trailing) { + return num_int64((int64_t)p_num) + ".0"; + } else { + return num_int64((int64_t)p_num); + } + } int decimals = 6; -#endif // We want to align the digits to the above sane default, so we only need // to subtract log10 for numbers with a positive power of ten magnitude. - double abs_num = Math::abs(p_num); + const float abs_num = Math::abs(p_num); if (abs_num > 10) { decimals -= (int)floor(log10(abs_num)); } @@ -4601,7 +4615,7 @@ String String::humanize_size(uint64_t p_size) { } if (magnitude == 0) { - return String::num(p_size) + " " + RTR("B"); + return String::num_uint64(p_size) + " " + RTR("B"); } else { String suffix; switch (magnitude) { diff --git a/core/string/ustring.h b/core/string/ustring.h index aa62c9cb18..6761a42579 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -332,6 +332,7 @@ public: static String num(double p_num, int p_decimals = -1); static String num_scientific(double p_num); static String num_real(double p_num, bool p_trailing = true); + static String num_real(float p_num, bool p_trailing = true); static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false); static String num_uint64(uint64_t p_num, int base = 10, bool capitalize_hex = false); static String chr(char32_t p_char); diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index e2865a06be..7550477d70 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -1736,7 +1736,7 @@ String Variant::stringify(int recursion_count) const { case INT: return itos(_data._int); case FLOAT: - return rtos(_data._float); + return String::num_real(_data._float, true); case STRING: return *reinterpret_cast<const String *>(_data._mem); case VECTOR2: |