Here's a neat way to always have nicely formatted money values!
Example: pawn Код:
Код:
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 Код:
|
#include <formatex>
Is there any way to use your formatex and the va_format from y_va together?
Cause I use SendClientMessageEx with va_format and I want to use your custom format in SCMEx. |
stock va_formatex(output[], size = sizeof(output), const fmat[], va_:STATIC_ARGS) {
new
num_args,
arg_start,
arg_end;
// Get the pointer to the number of arguments to the last function.
#emit LOAD.S.pri 0
#emit ADD.C 8
#emit MOVE.alt
// Get the number of arguments.
#emit LOAD.I
#emit STOR.S.pri num_args
// Get the variable arguments (end).
#emit ADD
#emit STOR.S.pri arg_end
// Get the variable arguments (start).
#emit LOAD.S.pri STATIC_ARGS
#emit SMUL.C 4
#emit ADD
#emit STOR.S.pri arg_start
// Using an assembly loop here screwed the code up as the labels added some
// odd stack/frame manipulation code...
while (arg_end != arg_start)
{
#emit MOVE.pri
#emit LOAD.I
#emit PUSH.pri
#emit CONST.pri 4
#emit SUB.alt
#emit STOR.S.pri arg_end
}
// Push the additional parameters.
#emit PUSH.S fmat
#emit PUSH.S size
#emit PUSH.S output
// Push the argument count.
#emit LOAD.S.pri num_args
#emit ADD.C 12
#emit LOAD.S.alt STATIC_ARGS
#emit XCHG
#emit SMUL.C 4
#emit SUB.alt
#emit PUSH.pri
#emit MOVE.alt
// Push the return address.
#emit LCTRL 6
#emit ADD.C 28
#emit PUSH.pri
// Call formatex
#emit CONST.pri formatex
#emit SCTRL 6
}
static stock
gs_Buffer[256]
;
stock SendClientMessagef(playerid, color, fmat[], va_args<>) {
va_formatex(gs_Buffer, _, fmat, va_start<3>);
SendClientMessage(playerid, color, gs_Buffer);
}
stock SendClientMessageToAllf(color, fmat[], va_args<>) {
va_formatex(gs_Buffer, _, fmat, va_start<2>);
SendClientMessageToAll(color, gs_Buffer);
}
color = 0xFFFFFFFF;
SendClientMessageToAllf(color, "* %P%C just joined the server.", playerid, color);
for (new i = 400; i < 611; i++) printf("%v", i);
stock mysql_formatex(connectionHandle, szOutput[], iLength = sizeof(szOutput), const szFormatString[], GLOBAL_TAG_TYPES:...) {
... Same Codes
// New format specifier
#emit PUSH.C s_szBuffer
// Max length
#emit PUSH.S iLength
// Output string
#emit PUSH.S szOutput
// Handle
#emit PUSH.S connectionHandle
// Argument count
#emit LOAD.S.pri iArgCount
#emit SHL.C.pri 2
#emit ADD.C 16
#emit PUSH.pri
// Save the argument count for later
#emit MOVE.alt
// Call format (duh)
#emit SYSREQ.C mysql_format
... Same More codes
#include <a_samp>
main() {
printf("Straight: '%q'", "AndySedeyn");
}
[11:49:32] Straight: 'AndySedeyn'
#include <a_samp>
#include <formatex>
main() {
printf("Straight: '%q'", "AndySedeyn");
}
[11:53:11] Straight: ''
// Handled by the original format function
case '*', 'i', 'd', 'x', 'h', 'c', 's', 'f', 'b', 'q': { // I added 'q'
// Get the argument address and save it for later
#emit LCTRL 5
#emit LOAD.S.alt iArg
#emit ADD
#emit LOAD.I
#emit STOR.S.pri iAddress
#emit MOVE.pri
#emit ADD.C 4
#emit STOR.S.pri iArg
aiArgs[iArgCount++] = iAddress;
if (s_szBuffer[iPos] == '*')
continue;
break;
}
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";
}
}