Skip to content

Commit 5c0f241

Browse files
committed
Always add decimal when printing float
1 parent 533c616 commit 5c0f241

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
@@ -1818,7 +1818,7 @@ String String::num(double p_num, int p_decimals) {
18181818
#endif
18191819

18201820
buf[324] = 0;
1821-
//destroy trailing zeroes
1821+
// Destroy trailing zeroes, except one after period.
18221822
{
18231823
bool period = false;
18241824
int z = 0;
@@ -1835,7 +1835,7 @@ String String::num(double p_num, int p_decimals) {
18351835
if (buf[z] == '0') {
18361836
buf[z] = 0;
18371837
} else if (buf[z] == '.') {
1838-
buf[z] = 0;
1838+
buf[z + 1] = '0';
18391839
break;
18401840
} else {
18411841
break;
@@ -1924,14 +1924,28 @@ String String::num_real(double p_num, bool p_trailing) {
19241924
return num_int64((int64_t)p_num);
19251925
}
19261926
}
1927-
#ifdef REAL_T_IS_DOUBLE
19281927
int decimals = 14;
1929-
#else
1928+
// We want to align the digits to the above sane default, so we only need
1929+
// to subtract log10 for numbers with a positive power of ten magnitude.
1930+
const double abs_num = Math::abs(p_num);
1931+
if (abs_num > 10) {
1932+
decimals -= (int)floor(log10(abs_num));
1933+
}
1934+
return num(p_num, decimals);
1935+
}
1936+
1937+
String String::num_real(float p_num, bool p_trailing) {
1938+
if (p_num == (float)(int64_t)p_num) {
1939+
if (p_trailing) {
1940+
return num_int64((int64_t)p_num) + ".0";
1941+
} else {
1942+
return num_int64((int64_t)p_num);
1943+
}
1944+
}
19301945
int decimals = 6;
1931-
#endif
19321946
// We want to align the digits to the above sane default, so we only need
19331947
// to subtract log10 for numbers with a positive power of ten magnitude.
1934-
double abs_num = Math::abs(p_num);
1948+
const float abs_num = Math::abs(p_num);
19351949
if (abs_num > 10) {
19361950
decimals -= (int)floor(log10(abs_num));
19371951
}
@@ -4601,7 +4615,7 @@ String String::humanize_size(uint64_t p_size) {
46014615
}
46024616

46034617
if (magnitude == 0) {
4604-
return String::num(p_size) + " " + RTR("B");
4618+
return String::num_uint64(p_size) + " " + RTR("B");
46054619
} else {
46064620
String suffix;
46074621
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)