MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / FormatNumber

Function FormatNumber

src/strings.cpp:482–508  ·  view source on GitHub ↗

* Format a number into a string. * @param builder the string builder to write to * @param number the number to write down * @param separator the thousands-separator to use */

Source from the content-addressed store, hash-verified

480 * @param separator the thousands-separator to use
481 */
482static void FormatNumber(StringBuilder &builder, int64_t number, std::string_view separator)
483{
484 static const int max_digits = 20;
485 uint64_t divisor = 10000000000000000000ULL;
486 int thousands_offset = (max_digits - 1) % 3;
487
488 if (number < 0) {
489 builder.PutChar('-');
490 number = -number;
491 }
492
493 uint64_t num = number;
494 uint64_t tot = 0;
495 for (int i = 0; i < max_digits; i++) {
496 uint64_t quot = 0;
497 if (num >= divisor) {
498 quot = num / divisor;
499 num = num % divisor;
500 }
501 if ((tot |= quot) || i == max_digits - 1) {
502 builder.PutChar('0' + quot); // quot is a single digit
503 if ((i % 3) == thousands_offset && i < max_digits - 1) builder += separator;
504 }
505
506 divisor /= 10;
507 }
508}
509
510static void FormatCommaNumber(StringBuilder &builder, int64_t number)
511{

Callers 2

FormatCommaNumberFunction · 0.85
FormatGenericCurrencyFunction · 0.85

Calls 1

PutCharMethod · 0.80

Tested by

no test coverage detected