1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-09 12:50:35 +00:00

Always add decimal when printing float

This commit is contained in:
kobewi
2024-03-26 17:04:14 +01:00
parent 533c616cb8
commit 5c0f2414cd
31 changed files with 241 additions and 141 deletions

View File

@@ -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
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 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;
// 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 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) {