Changeset - r28767:a4db692758d3
[Not reviewed]
master
0 1 0
Rubidium - 10 months ago 2024-02-08 19:37:55
rubidium@openttd.org
Codechange: Move determining the decimal separator to a separate function
1 file changed with 10 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/strings.cpp
Show inline comments
 
@@ -377,6 +377,13 @@ void SetDParamStr(size_t n, std::string 
 
	_global_string_params.SetParam(n, std::move(str));
 
}
 

	
 
static const char *GetDecimalSeparator()
 
{
 
	const char *decimal_separator = _settings_game.locale.digit_decimal_separator.c_str();
 
	if (StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
 
	return decimal_separator;
 
}
 

	
 
/**
 
 * Format a number into a string.
 
 * @param builder   the string builder to write to
 
@@ -403,9 +410,7 @@ static void FormatNumber(StringBuilder &
 
	uint64_t tot = 0;
 
	for (int i = 0; i < max_digits; i++) {
 
		if (i == max_digits - fractional_digits) {
 
			const char *decimal_separator = _settings_game.locale.digit_decimal_separator.c_str();
 
			if (StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
 
			builder += decimal_separator;
 
			builder += GetDecimalSeparator();
 
		}
 

	
 
		uint64_t quot = 0;
 
@@ -461,16 +466,13 @@ static void FormatBytes(StringBuilder &b
 
		id++;
 
	}
 

	
 
	const char *decimal_separator = _settings_game.locale.digit_decimal_separator.c_str();
 
	if (StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
 

	
 
	if (number < 1024) {
 
		id = 0;
 
		fmt::format_to(builder, "{}", number);
 
	} else if (number < 1024 * 10) {
 
		fmt::format_to(builder, "{}{}{:02}", number / 1024, decimal_separator, (number % 1024) * 100 / 1024);
 
		fmt::format_to(builder, "{}{}{:02}", number / 1024, GetDecimalSeparator(), (number % 1024) * 100 / 1024);
 
	} else if (number < 1024 * 100) {
 
		fmt::format_to(builder, "{}{}{:01}", number / 1024, decimal_separator, (number % 1024) * 10 / 1024);
 
		fmt::format_to(builder, "{}{}{:01}", number / 1024, GetDecimalSeparator(), (number % 1024) * 10 / 1024);
 
	} else {
 
		assert(number < 1024 * 1024);
 
		fmt::format_to(builder, "{}", number / 1024);
0 comments (0 inline, 0 general)