Originally Posted by Slice
Here's a neat way to always have nicely formatted money values!
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);
Output:
Код:
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
Specifier code:
pawn Код:
// Money FormatSpecifier<'m'>(output[], amount) { if (amount) { new i = 18 ; // The spaces must be there, otherwise strdel below won't work // on lower numbers. output = "$ "; // Null-out the end of it output[i] = 0; // 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); } else { output = "$0"; } }
|