Skip to content

Commit 363c0b5

Browse files
committed
Merge pull request godotengine#47502 from KoBeWi/add_0
Always add decimal when converting float to string
2 parents 7982030 + 5c0f241 commit 363c0b5

31 files changed

+240
-140
lines changed

core/extension/extension_api_dump.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,9 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_
13711371
Dictionary elem = var;
13721372
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));
13731373
String name = elem[p_name_field];
1374+
if (name.is_valid_float()) {
1375+
name = name.trim_suffix(".0"); // Make "integers" stringified as integers.
1376+
}
13741377
if (p_compare_operators && elem.has("right_type")) {
13751378
name += " " + String(elem["right_type"]);
13761379
}
@@ -1386,6 +1389,9 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_
13861389
continue;
13871390
}
13881391
String name = old_elem[p_name_field];
1392+
if (name.is_valid_float()) {
1393+
name = name.trim_suffix(".0"); // Make "integers" stringified as integers.
1394+
}
13891395
if (p_compare_operators && old_elem.has("right_type")) {
13901396
name += " " + String(old_elem["right_type"]);
13911397
}

core/math/rect2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_re
283283
}
284284

285285
Rect2::operator String() const {
286-
return "[P: " + position.operator String() + ", S: " + size + "]";
286+
return "[P: " + position.operator String() + ", S: " + size.operator String() + "]";
287287
}
288288

289289
Rect2::operator Rect2i() const {

core/math/vector2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ bool Vector2::is_finite() const {
203203
}
204204

205205
Vector2::operator String() const {
206-
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ")";
206+
return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ")";
207207
}
208208

209209
Vector2::operator Vector2i() const {

core/math/vector3.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ bool Vector3::is_finite() const {
165165
}
166166

167167
Vector3::operator String() const {
168-
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ")";
168+
return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ", " + String::num_real(z, true) + ")";
169169
}
170170

171171
Vector3::operator Vector3i() const {

core/string/ustring.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,7 @@ String String::num(double p_num, int p_decimals) {
18191819
#endif
18201820

18211821
buf[324] = 0;
1822-
//destroy trailing zeroes
1822+
// Destroy trailing zeroes, except one after period.
18231823
{
18241824
bool period = false;
18251825
int z = 0;
@@ -1836,7 +1836,7 @@ String String::num(double p_num, int p_decimals) {
18361836
if (buf[z] == '0') {
18371837
buf[z] = 0;
18381838
} else if (buf[z] == '.') {
1839-
buf[z] = 0;
1839+
buf[z + 1] = '0';
18401840
break;
18411841
} else {
18421842
break;
@@ -1929,14 +1929,28 @@ String String::num_real(double p_num, bool p_trailing) {
19291929
return num_int64((int64_t)p_num);
19301930
}
19311931
}
1932-
#ifdef REAL_T_IS_DOUBLE
19331932
int decimals = 14;
1934-
#else
1933+
// We want to align the digits to the above sane default, so we only need
1934+
// to subtract log10 for numbers with a positive power of ten magnitude.
1935+
const double abs_num = Math::abs(p_num);
1936+
if (abs_num > 10) {
1937+
decimals -= (int)floor(log10(abs_num));
1938+
}
1939+
return num(p_num, decimals);
1940+
}
1941+
1942+
String String::num_real(float p_num, bool p_trailing) {
1943+
if (p_num == (float)(int64_t)p_num) {
1944+
if (p_trailing) {
1945+
return num_int64((int64_t)p_num) + ".0";
1946+
} else {
1947+
return num_int64((int64_t)p_num);
1948+
}
1949+
}
19351950
int decimals = 6;
1936-
#endif
19371951
// We want to align the digits to the above sane default, so we only need
19381952
// to subtract log10 for numbers with a positive power of ten magnitude.
1939-
double abs_num = Math::abs(p_num);
1953+
const float abs_num = Math::abs(p_num);
19401954
if (abs_num > 10) {
19411955
decimals -= (int)floor(log10(abs_num));
19421956
}
@@ -4616,7 +4630,7 @@ String String::humanize_size(uint64_t p_size) {
46164630
}
46174631

46184632
if (magnitude == 0) {
4619-
return String::num(p_size) + " " + RTR("B");
4633+
return String::num_uint64(p_size) + " " + RTR("B");
46204634
} else {
46214635
String suffix;
46224636
switch (magnitude) {

core/string/ustring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ class String {
332332
static String num(double p_num, int p_decimals = -1);
333333
static String num_scientific(double p_num);
334334
static String num_real(double p_num, bool p_trailing = true);
335+
static String num_real(float p_num, bool p_trailing = true);
335336
static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false);
336337
static String num_uint64(uint64_t p_num, int base = 10, bool capitalize_hex = false);
337338
static String chr(char32_t p_char);

core/variant/variant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ String Variant::stringify(int recursion_count) const {
17361736
case INT:
17371737
return itos(_data._int);
17381738
case FLOAT:
1739-
return rtos(_data._float);
1739+
return String::num_real(_data._float, true);
17401740
case STRING:
17411741
return *reinterpret_cast<const String *>(_data._mem);
17421742
case VECTOR2:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
GDTEST_OK
2-
0
2+
0.0

modules/gdscript/tests/scripts/analyzer/features/const_conversions.gd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ const const_packed_ints: PackedFloat64Array = [52]
77

88
func test():
99
Utils.check(typeof(const_float_int) == TYPE_FLOAT)
10-
Utils.check(str(const_float_int) == '19')
10+
Utils.check(str(const_float_int) == '19.0')
1111
Utils.check(typeof(const_float_plus) == TYPE_FLOAT)
12-
Utils.check(str(const_float_plus) == '34')
12+
Utils.check(str(const_float_plus) == '34.0')
1313
Utils.check(typeof(const_float_cast) == TYPE_FLOAT)
14-
Utils.check(str(const_float_cast) == '76')
14+
Utils.check(str(const_float_cast) == '76.0')
1515

1616
Utils.check(typeof(const_packed_empty) == TYPE_PACKED_FLOAT64_ARRAY)
1717
Utils.check(str(const_packed_empty) == '[]')
1818
Utils.check(typeof(const_packed_ints) == TYPE_PACKED_FLOAT64_ARRAY)
19-
Utils.check(str(const_packed_ints) == '[52]')
19+
Utils.check(str(const_packed_ints) == '[52.0]')
2020
Utils.check(typeof(const_packed_ints[0]) == TYPE_FLOAT)
21-
Utils.check(str(const_packed_ints[0]) == '52')
21+
Utils.check(str(const_packed_ints[0]) == '52.0')
2222

2323
print('ok')
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
GDTEST_OK
2-
4
2+
4.0

0 commit comments

Comments
 (0)