Raised This Month: $ Target: $400
 0% 

IntToMoney


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Twelve-60
Senior Member
Join Date: Aug 2007
Old 08-27-2007 , 18:35   IntToMoney
Reply With Quote #1

Edit: Deleted - terrible code :p

- Twelve-60

Last edited by Twelve-60; 05-02-2008 at 15:10.
Twelve-60 is offline
Nican
Veteran Member
Join Date: Jan 2006
Location: NY
Old 09-04-2007 , 01:44   Re: IntToMoney
Reply With Quote #2

Sorry twelve, but I just could not live with that script :O

I developed a new way,
PHP Code:
stock IntToMoney(theintString:result[], maxlen){
     new 
slenpointerString:intstr[maxlen], bool:negative;
     
     
negative theint 0;
     if(
negativetheint *= -1;
     
     
IntToString(theintintstrmaxlen);
     
slen strlen(intstr);
     
     
theint slen 3;
     if(
theint == 0theint 3;
    
Format(result,theint 1"%s"intstr);
    
    
slen -= theint;
    
pointer theint 1;
    for(new 
theint<= slen += 3){
         
pointer += 4;
        
Format(resultpointer"%s,%s",resultintstr[i]);
    }
     
     if(
negative)
         
Format(resultmaxlen"$-%s"result);
     else
         
Format(resultmaxlen"$%s"result);


And, here is a very good exemple:
PHP Code:
public OnPluginStart()
{
    new 
String:result[32];
    
    
IntToMoney(1,result,32);
    
LogMessage("%s",result);
    
IntToMoney(12,result,32);
    
LogMessage("%s",result);
    
IntToMoney(123,result,32);
    
LogMessage("%s",result);
    
IntToMoney(1234,result,32);
    
LogMessage("%s",result);
    
IntToMoney(12345,result,32);
    
LogMessage("%s",result);
    
IntToMoney(123456,result,32);
    
LogMessage("%s",result);
    
IntToMoney(1234567,result,32);
    
LogMessage("%s",result);
    
IntToMoney(12345678,result,32);
    
LogMessage("%s",result);
    
IntToMoney(-123456789,result,32);
    
LogMessage("%s",result);
    
IntToMoney(1234567890,result,32);
    
LogMessage("%s",result);

Gave me:
PHP Code:
L 09/04/2007 01:43:11: [nican.smx] $1
L 09
/04/2007 01:43:11: [nican.smx] $12
L 09
/04/2007 01:43:11: [nican.smx] $123
L 09
/04/2007 01:43:11: [nican.smx] $1,234
L 09
/04/2007 01:43:11: [nican.smx] $12,345
L 09
/04/2007 01:43:11: [nican.smx] $123,456
L 09
/04/2007 01:43:11: [nican.smx] $1,234,567
L 09
/04/2007 01:43:11: [nican.smx] $12,345,678
L 09
/04/2007 01:43:11: [nican.smx] $-123,456,789
L 09
/04/2007 01:43:11: [nican.smx] $1,234,567,890 
__________________
http://www.nican132.com
I require reputation!

Last edited by Nican; 09-04-2007 at 11:06.
Nican is offline
Send a message via ICQ to Nican Send a message via MSN to Nican
dalto
Veteran Member
Join Date: Jul 2007
Old 09-04-2007 , 09:31   Re: IntToMoney
Reply With Quote #3

Well, since everyone is sharing their money formatting code, I have a stock I for this as well.

Code:
stock FormatMoney(String:moneyString[], moneySize)
{
    if(strlen(moneyString) <= 3)
    {
        Format(moneyString, moneySize, "$%s", moneyString);
        return;
    }
    new String:buffer[20];
    strcopy(buffer, sizeof(buffer), moneyString[strlen(moneyString) - 3]);
    moneyString[strlen(moneyString) - 3] = 0;
    while(strlen(moneyString) > 3)
    {
        Format(buffer, sizeof(buffer), "%s,%s", moneyString[strlen(moneyString) - 3], buffer);
        moneyString[strlen(moneyString) - 3] = 0;
    }
    Format(moneyString, moneySize, "$%s,%s", moneyString, buffer);
}

Last edited by dalto; 09-04-2007 at 19:34.
dalto is offline
Nican
Veteran Member
Join Date: Jan 2006
Location: NY
Old 09-04-2007 , 10:39   Re: IntToMoney
Reply With Quote #4

dalto, your script has some bugs (unexpected features) ... Look at what it gave me:

PHP Code:
L 09/04/2007 10:36:59: [nican.smx] $1,
L 09/04/2007 10:36:59: [nican.smx] $12,
L 09/04/2007 10:36:59: [nican.smx] $,123
L 09
/04/2007 10:36:59: [nican.smx] $1,234
L 09
/04/2007 10:36:59: [nican.smx] $12,345
L 09
/04/2007 10:36:59: [nican.smx] $123,456
L 09
/04/2007 10:36:59: [nican.smx] $1,234,567
L 09
/04/2007 10:36:59: [nican.smx] $12,345,678
L 09
/04/2007 10:36:59: [nican.smx] $-,123,456,789
L 09
/04/2007 10:36:59: [nican.smx] $1,234,567,890 
__________________
http://www.nican132.com
I require reputation!
Nican is offline
Send a message via ICQ to Nican Send a message via MSN to Nican
dalto
Veteran Member
Join Date: Jul 2007
Old 09-04-2007 , 12:22   Re: IntToMoney
Reply With Quote #5

Quote:
Originally Posted by Nican View Post
dalto, your script has some bugs (unexpected features)
Good catch, updated to handle smaller numbers properly. It is not intended to handle negative numbers, although it could be adapted trivially, what I wrote it for had no need for negative number handling.
dalto is offline
Twelve-60
Senior Member
Join Date: Aug 2007
Old 09-04-2007 , 19:02   Re: IntToMoney
Reply With Quote #6

(This was made before I learnt of the Format() function )

Ty anyway

- Twelve-60
Twelve-60 is offline
Nican
Veteran Member
Join Date: Jan 2006
Location: NY
Old 09-04-2007 , 21:00   Re: IntToMoney
Reply With Quote #7

:O!!!!

if you look into string.inc, you will find:
PHP Code:
/**
 * Concatenates one string onto another.
 *
 * @param buffer        String to append to.
 * @param maxlength        Maximum length of entire buffer.
 * @param source        Source string to concatenate.
 * @return                Number of bytes written.
 */
stock StrCat(String:buffer[], maxlength, const String:source[])
{
    new 
len strlen(buffer);
    if (
len >= maxlength)
    {
        return 
0;
    }
    
    return 
Format(buffer[len], maxlength-len"%s"source);


__________________
http://www.nican132.com
I require reputation!
Nican is offline
Send a message via ICQ to Nican Send a message via MSN to Nican
Master53
Veteran Member
Join Date: Dec 2009
Old 01-11-2010 , 12:44   Re: IntToMoney
Reply With Quote #8

nvm

Last edited by Master53; 07-19-2010 at 16:58.
Master53 is offline
Sammy-ROCK!
Senior Member
Join Date: Jun 2008
Location: Near Mrs.Lag
Old 01-11-2010 , 14:21   Re: IntToMoney
Reply With Quote #9

Why not a float for this?
Sammy-ROCK! is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:57.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode