29.01.2012, 13:32
(
Последний раз редактировалось Slice; 23.04.2012 в 10:59.
)
Here's a neat way to always have nicely formatted money values!
Example:
Output:
Specifier code:
Example:
pawn Код:
printf("You just earned %m. You now have a total of %m in your bank.", 12000, 554279811);
printf("%m", 1000000000);
printf("%m", 100000000);
printf("%m", 10000000);
printf("%m", 1000000);
printf("%m", 100000);
printf("%m", 10000);
printf("%m", 1000);
printf("%m", 100);
printf("%m", 10);
printf("%m", 1);
printf("%m", 0);
Код:
You just earned $12,000. You now have a total of $554,279,811 in your bank. $1,000,000,000 $100,000,000 $10,000,000 $1,000,000 $100,000 $10,000 $1,000 $100 $10 $1 $0
pawn Код:
// Money
FormatSpecifier<'m'>(output[], amount) {
if (amount) {
new
i = 18,
neg = amount < 0
;
// The spaces must be there, otherwise strdel below won't work
// on lower numbers.
output = "$ ";
// Null-out the end of it
output[i] = 0;
if (neg)
amount = -amount;
// Going right-left, add one number each time
while (amount) {
// Add a thousand separator every 3rd time
if (!((i + 1) % 4))
output[--i] = ',';
// Now add the last digit of the number
output[--i] = '0' + (amount % 10);
// Then divide the number by 10, so digit in the end will be gone
amount /= 10;
}
// Delete the spaces between the $-sign and the first (last) number
strdel(output, 1, i);
// Add a minus sign if needed
if (neg)
strins(output, "-", 1);
} else {
output = "$0";
}
}