01.06.2017, 21:07
How can i add number comma separator in-game ?
Example : $1200000
to
$1,200,000 ?
Example : $1200000
to
$1,200,000 ?
stock FormatNumber(integer, const separator[] = ",")
{
new string[16];
valstr(string, integer);
if(integer >= 1000)
{
for(new i = (strlen(string) - 3); i > 0; i -= 3)
{
strins(string, separator, i);
}
}
return string;
}
new x=1; new str[128]; format(str,sizeof(str), "%s", FormatNumber(x)); printf(str);
stock FormatNumber(number) { new Str[15]; format(Str, 15, "%d", number); if (strlen(Str) < sizeof(Str)) { if (number >= 1000 && number < 10000 || number >= -1000 && number < -10000) strins( Str, ",", 1, sizeof(Str)); else if (number >= 10000 && number < 100000 || number >= -10000 && number < -100000) strins(Str, ",", 2, sizeof(Str)); else if (number >= 100000 && number < 1000000 || number >= -100000 && number < -1000000) strins(Str, ",", 3, sizeof(Str)); else if (number >= 1000000 && number < 10000000 || number >= -1000000 && number < -10000000) strins(Str, ",", 1, sizeof(Str)),strins(Str, ",", 5, sizeof(Str)); else if (number >= 10000000 && number < 100000000 || number >= -10000000 && number < -100000000) strins(Str, ",", 2, sizeof(Str)),strins(Str, ",", 6, sizeof(Str)); else if (number >= 100000000 && number < 1000000000 || number >= -100000000 && number < -1000000000) strins(Str, ",", 3, sizeof(Str)),strins(Str, ",", 7, sizeof(Str)); else if (number >= 1000000000 && number < 10000000000 || number >= -1000000000 && number < -10000000000) strins(Str, ",", 1, sizeof(Str)), strins(Str, ",", 5, sizeof(Str)), strins(Str, ",", 9, sizeof(Str)); else format(Str, 10, "%d", number); } else format( Str, 15, "<BUG>" ); return Str; }
Try this:
Code:
stock FormatNumber(number) { new Str[15]; format(Str, 15, "%d", number); if (strlen(Str) < sizeof(Str)) { if (number >= 1000 && number < 10000 || number >= -1000 && number < -10000) strins( Str, ",", 1, sizeof(Str)); else if (number >= 10000 && number < 100000 || number >= -10000 && number < -100000) strins(Str, ",", 2, sizeof(Str)); else if (number >= 100000 && number < 1000000 || number >= -100000 && number < -1000000) strins(Str, ",", 3, sizeof(Str)); else if (number >= 1000000 && number < 10000000 || number >= -1000000 && number < -10000000) strins(Str, ",", 1, sizeof(Str)),strins(Str, ",", 5, sizeof(Str)); else if (number >= 10000000 && number < 100000000 || number >= -10000000 && number < -100000000) strins(Str, ",", 2, sizeof(Str)),strins(Str, ",", 6, sizeof(Str)); else if (number >= 100000000 && number < 1000000000 || number >= -100000000 && number < -1000000000) strins(Str, ",", 3, sizeof(Str)),strins(Str, ",", 7, sizeof(Str)); else if (number >= 1000000000 && number < 10000000000 || number >= -1000000000 && number < -10000000000) strins(Str, ",", 1, sizeof(Str)), strins(Str, ",", 5, sizeof(Str)), strins(Str, ",", 9, sizeof(Str)); else format(Str, 10, "%d", number); } else format( Str, 15, "<BUG>" ); return Str; } |
Try this:
Code:
stock FormatNumber(number) { new Str[15]; format(Str, 15, "%d", number); if (strlen(Str) < sizeof(Str)) { if (number >= 1000 && number < 10000 || number >= -1000 && number < -10000) strins( Str, ",", 1, sizeof(Str)); else if (number >= 10000 && number < 100000 || number >= -10000 && number < -100000) strins(Str, ",", 2, sizeof(Str)); else if (number >= 100000 && number < 1000000 || number >= -100000 && number < -1000000) strins(Str, ",", 3, sizeof(Str)); else if (number >= 1000000 && number < 10000000 || number >= -1000000 && number < -10000000) strins(Str, ",", 1, sizeof(Str)),strins(Str, ",", 5, sizeof(Str)); else if (number >= 10000000 && number < 100000000 || number >= -10000000 && number < -100000000) strins(Str, ",", 2, sizeof(Str)),strins(Str, ",", 6, sizeof(Str)); else if (number >= 100000000 && number < 1000000000 || number >= -100000000 && number < -1000000000) strins(Str, ",", 3, sizeof(Str)),strins(Str, ",", 7, sizeof(Str)); else if (number >= 1000000000 && number < 10000000000 || number >= -1000000000 && number < -10000000000) strins(Str, ",", 1, sizeof(Str)), strins(Str, ",", 5, sizeof(Str)), strins(Str, ",", 9, sizeof(Str)); else format(Str, 10, "%d", number); } else format( Str, 15, "<BUG>" ); return Str; } |
YEs,but i don't know how to add separator per numbers, i added this code on my GM and no effect ?
|
public OnPlayerConnect(playerid)
{
new string[144], var = 215161241;
format(string, sizeof(string), "Without the use of function: %d - With prior use: %s", var, FormatNumber(var));
SendClientMessage(playerid, -1, string);
return 1;
}
FormatNumber(integer, const separator[] = ",")
{
new string[16];
valstr(string, integer);
if(integer >= 1000)
{
for(new i = (strlen(string) - 3); i > 0; i -= 3)
{
strins(string, separator, i);
}
}
return string;
}
Don't use his code, it's rubbish.
Here's an example: pawn Code:
If you still don't know how to use the function then there is absolutely no way to help you any further. |
format(string, 256, "Money: $%d", FormatNumber(money));
format(string, 256, "Money: $%s", FormatNumber(money));