01.04.2016, 16:06
I had once posted a small function to format integer to currency. This is an improved version of my previous code;
Function will return a string containing currency formatted integer.
pawn Код:
//charins - To insert a character to an array.
stock charins(string[], char_, pos, len = -1) {
if(len == -1)
len = strlen(string);
for(--len; len >= pos; len--)
string[len+1] = string[len];
string[pos] = char_;
return 1;
}
stock currency_format(number, curr = '$', delim = ',') {
new
temp_Str[32],
temp_Len = 0
;
valstr(temp_Str, number, false);
temp_Len = strlen(temp_Str);
charins(temp_Str, curr, 0, temp_Len++);
if(number > 999) {
new
i = temp_Len - 3;
do {
charins(temp_Str, delim, i, temp_Len++);
i -= 3;
}
while(i >= 2);
}
else if(number < -999) {
new
i = temp_Len - 3;
do {
charins(temp_Str, delim, i, temp_Len++);
i -= 3;
}
while(i >= 3);
}
return temp_Str;
}