Float money
#1

How would i format cash to say like : 1.500.454.435 etc?

I saw on useful functions section a function named NiceMoney, but this didn't work ?

heres the NiceMoney function:

pawn Код:
NiceMoney(amount, sep[] = ".")
{
    new
        money[16],
        negativ = 0;
    if(amount < 0) negativ = 1;
    format(money, sizeof(money), "%i", amount);
    new
        lenght = strlen(money);
    while((lenght -= 3) > negativ) strins(money, sep, lenght);
    return money;
}
Also tested it with a loop:
pawn Код:
new Test[5];
    for(new i; i < sizeof(Test); i++)
    {
        Test[i] = rand(50000,150000);
        printf("%f",NiceMoney(Test[i]));
    }
Reply
#2

pawn Код:
stock NiceMoney(amount)
{
    new money[16],negativ = 0, sep[2], tmp; sep= ".";
    if(amount < 0) negativ = 1;
    format(money, sizeof(money), "%d", amount);
    for(new i=strlen(money); i>negativ+1; i--)
    {
        tmp++;
        if(tmp==3) { strins(money, sep, i-1); tmp=0; }
    }
    return money;
}
Usage example:
pawn Код:
printf("%s", NiceMoney(1234567890));
I hope this helps.
Reply
#3

Quote:
Originally Posted by Jeffry
Посмотреть сообщение
pawn Код:
stock NiceMoney(amount)
{
    new money[16],negativ = 0, sep[2], tmp; sep= ".";
    if(amount < 0) negativ = 1;
    format(money, sizeof(money), "%d", amount);
    for(new i=strlen(money); i>negativ+1; i--)
    {
        tmp++;
        if(tmp==3) { strins(money, sep, i-1); tmp=0; }
    }
    return money;
}
Usage example:
pawn Код:
printf("%s", NiceMoney(1234567890));
I hope this helps.
Yeah, let me try ^^
Reply
#4

pawn Код:
// number_format function by Slice i think
stock number_format( num )
{
    new stri[16], stro[16], i, v, p, d, l, n = num < 0;
    format( stri, sizeof( stri ), "%d", num * ( n ? -1 : 1 ) );

    l = strlen( stri ) - 1;
    d = ( l - ( l % 3 ) ) / 3;
    l = l + 1;
    i = l + d;
    p = l;
    while ( i >= 0 )
    {
        v = l + d - i;
        if ( v && !( v % 4 ) )
            stro[i + n] = ',';
        else
            stro[i + n] = stri[p--];
        i--;
    }
    stro[0] = n ? '-' : stro[0];
    return stro;
}
Reply
#5

None of them does work :S..

Lol their a string ! not a int.. xD thanks for help!
Reply
#6

Quote:
Originally Posted by Cameltoe
Посмотреть сообщение
None of them does work :S..
O.o I tested it. :/

Show your code how you use it please.
Reply
#7

Quote:
Originally Posted by Jeffry
Посмотреть сообщение
O.o I tested it. :/

Show your code how you use it please.
Work's now i thought the number's where int's but there were strings lol thanks^^
Reply
#8

Quote:
Originally Posted by exora
Посмотреть сообщение
pawn Код:
// number_format function by Slice i think
stock number_format( num )
{
    new stri[16], stro[16], i, v, p, d, l, n = num < 0;
    format( stri, sizeof( stri ), "%d", num * ( n ? -1 : 1 ) );

    l = strlen( stri ) - 1;
    d = ( l - ( l % 3 ) ) / 3;
    l = l + 1;
    i = l + d;
    p = l;
    while ( i >= 0 )
    {
        v = l + d - i;
        if ( v && !( v % 4 ) )
            stro[i + n] = ',';
        else
            stro[i + n] = stri[p--];
        i--;
    }
    stro[0] = n ? '-' : stro[0];
    return stro;
}
yeah it is lol. haven't seen that in ages, where'd you find it?
Reply
#9

Quote:
Originally Posted by Cameltoe
Посмотреть сообщение
Work's now i thought the number's where int's but there were strings lol thanks^^
Great. Thanks for the feedback.

Have fun.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)