(svn r22304) -Add: {DECIMAL} string code to print decimal fractions.

This commit is contained in:
frosch 2011-04-09 20:26:14 +00:00
parent 2c8b7a980f
commit 70f9a6747e
3 changed files with 23 additions and 5 deletions

View File

@ -229,14 +229,17 @@ void InjectDParam(uint amount)
* @param number the number to write down * @param number the number to write down
* @param last the last element in the buffer * @param last the last element in the buffer
* @param separator the thousands-separator to use * @param separator the thousands-separator to use
* @param zerofill minimum number of digits to print. The number will be filled with zeros at the front if necessary. * @param zerofill minimum number of digits to print for the integer part. The number will be filled with zeros at the front if necessary.
* @param fractional_digits number of fractional digits to display after a decimal separator. The decimal separator is inserted
* in front of the \a fractional_digits last digit of \a number.
* @return till where we wrote * @return till where we wrote
*/ */
static char *FormatNumber(char *buff, int64 number, const char *last, const char *separator, int zerofill = 1) static char *FormatNumber(char *buff, int64 number, const char *last, const char *separator, int zerofill = 1, int fractional_digits = 0)
{ {
static const int max_digits = 20; static const int max_digits = 20;
uint64 divisor = 10000000000000000000ULL; uint64 divisor = 10000000000000000000ULL;
int thousands_offset = (max_digits - 1) % 3; zerofill += fractional_digits;
int thousands_offset = (max_digits - fractional_digits - 1) % 3;
if (number < 0) { if (number < 0) {
buff += seprintf(buff, last, "-"); buff += seprintf(buff, last, "-");
@ -246,6 +249,12 @@ static char *FormatNumber(char *buff, int64 number, const char *last, const char
uint64 num = number; uint64 num = number;
uint64 tot = 0; uint64 tot = 0;
for (int i = 0; i < max_digits; i++) { for (int i = 0; i < max_digits; i++) {
if (i == max_digits - fractional_digits) {
const char *decimal_separator = _settings_game.locale.digit_decimal_separator;
if (decimal_separator == NULL) decimal_separator = _langpack->digit_decimal_separator;
buff += seprintf(buff, last, "%s", decimal_separator);
}
uint64 quot = 0; uint64 quot = 0;
if (num >= divisor) { if (num >= divisor) {
quot = num / divisor; quot = num / divisor;
@ -264,11 +273,11 @@ static char *FormatNumber(char *buff, int64 number, const char *last, const char
return buff; return buff;
} }
static char *FormatCommaNumber(char *buff, int64 number, const char *last) static char *FormatCommaNumber(char *buff, int64 number, const char *last, int fractional_digits = 0)
{ {
const char *separator = _settings_game.locale.digit_group_separator; const char *separator = _settings_game.locale.digit_group_separator;
if (separator == NULL) separator = _langpack->digit_group_separator; if (separator == NULL) separator = _langpack->digit_group_separator;
return FormatNumber(buff, number, last, separator); return FormatNumber(buff, number, last, separator, 1, fractional_digits);
} }
static char *FormatNoCommaNumber(char *buff, int64 number, const char *last) static char *FormatNoCommaNumber(char *buff, int64 number, const char *last)
@ -997,6 +1006,13 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg
buff = FormatCommaNumber(buff, args->GetInt64(SCC_COMMA), last); buff = FormatCommaNumber(buff, args->GetInt64(SCC_COMMA), last);
break; break;
case SCC_DECIMAL: {// {DECIMAL}
int64 number = args->GetInt64(SCC_DECIMAL);
int digits = args->GetInt32(SCC_DECIMAL);
buff = FormatCommaNumber(buff, number, last, digits);
break;
}
case SCC_ARG_INDEX: { // Move argument pointer case SCC_ARG_INDEX: { // Move argument pointer
args->offset = orig_offset + (byte)*str++; args->offset = orig_offset + (byte)*str++;
break; break;

View File

@ -72,6 +72,7 @@ enum StringControlCode {
SCC_STRING, SCC_STRING,
SCC_COMMA, SCC_COMMA,
SCC_DECIMAL,
SCC_NUM, SCC_NUM,
SCC_ZEROFILL_NUM, SCC_ZEROFILL_NUM,
SCC_HEX, SCC_HEX,

View File

@ -92,6 +92,7 @@ static const CmdStruct _cmd_structs[] = {
/* Numbers */ /* Numbers */
{"COMMA", EmitSingleChar, SCC_COMMA, 1, C_NONE}, // Number with comma {"COMMA", EmitSingleChar, SCC_COMMA, 1, C_NONE}, // Number with comma
{"DECIMAL", EmitSingleChar, SCC_DECIMAL, 2, C_NONE}, // Number with comma and fractional part. Second parameter is number of fractional digits, first parameter is number times 10**(second parameter).
{"NUM", EmitSingleChar, SCC_NUM, 1, C_NONE}, // Signed number {"NUM", EmitSingleChar, SCC_NUM, 1, C_NONE}, // Signed number
{"ZEROFILL_NUM", EmitSingleChar, SCC_ZEROFILL_NUM, 2, C_NONE}, // Unsigned number with zero fill, e.g. "02". First parameter is number, second minimum length {"ZEROFILL_NUM", EmitSingleChar, SCC_ZEROFILL_NUM, 2, C_NONE}, // Unsigned number with zero fill, e.g. "02". First parameter is number, second minimum length
{"BYTES", EmitSingleChar, SCC_BYTES, 1, C_NONE}, // Unsigned number with "bytes", i.e. "1.02 MiB or 123 KiB" {"BYTES", EmitSingleChar, SCC_BYTES, 1, C_NONE}, // Unsigned number with "bytes", i.e. "1.02 MiB or 123 KiB"