Raised This Month: $ Target: $400
 0% 

Int Formatting


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hazukiy
Member
Join Date: Jul 2009
Location: England
Old 03-15-2015 , 08:14   Int Formatting
Reply With Quote #1

Hi, so I'm trying to figure out how I can format money in my mod. Basically I have a money mod that's printing the money to the hud but I'd like it so that instead of just having loads of numbers ($20543589"), I'd like it to look something like "$20,543,589".

I would expect you would have to do something like convert it to a string and then use some method to say for every 3 bits of data apply a comma but I'm a bit iffy on how I could do that.

Here's the code for the hud:

PHP Code:
SetHudTextParams(0.015, -1.0HoldTimeHudColor);
ShowHudText(Client, -1"[%s]\nMoney: $%i\nPayCheck: $%i\nKills: %i\nLevel: %i"ClientNameg_iMoney[Client], iPayCheckg_iTotalKills[Client], g_iLevel[Client]); 
This is what the game displays:
Spoiler

Last edited by hazukiy; 03-15-2015 at 11:57.
hazukiy is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 03-15-2015 , 08:30   Re: Int Formatting
Reply With Quote #2

Can we see what the game displays?
__________________
WildCard65 is offline
hazukiy
Member
Join Date: Jul 2009
Location: England
Old 03-15-2015 , 08:46   Re: Int Formatting
Reply With Quote #3

Quote:
Originally Posted by WildCard65 View Post
Can we see what the game displays?
Ok done, sorry that's it's so big, I have no idea why it's not scaling down.
hazukiy is offline
Wliu
Veteran Member
Join Date: Apr 2013
Old 03-15-2015 , 10:44   Re: Int Formatting
Reply With Quote #4

@hazukiy you could wrap it in [spoiler]<put image here>[/spoiler] tags.

For your question you'd probably need to:
1) IntToString your money integer
2) Create another string that has sizeof(string1)+RoundFloat(sizeof(string1)/3)
3) Have a for loop that counts backwards that reads data from string1 and puts it into string2 while also inserting commas every 3 loops.

This looks right to me right now but I could be overlooking something.
__________________
~Wliu
Wliu is offline
hazukiy
Member
Join Date: Jul 2009
Location: England
Old 03-15-2015 , 12:07   Re: Int Formatting
Reply With Quote #5

Quote:
Originally Posted by Wliu View Post
@hazukiy you could wrap it in [spoiler]<put image here>[/spoiler] tags.

For your question you'd probably need to:
1) IntToString your money integer
2) Create another string that has sizeof(string1)+RoundFloat(sizeof(string1)/3)
3) Have a for loop that counts backwards that reads data from string1 and puts it into string2 while also inserting commas every 3 loops.

This looks right to me right now but I could be overlooking something.
Right so I've got this far but I'm a little confused what you mean on the third part.

PHP Code:
for(new 30i--)
{
IntToString(g_iMoney[Client], sBuffersizeof(sBuffer)+RoundFloat(sizeof(sBuffer)/3));

hazukiy is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 03-16-2015 , 14:46   Re: Int Formatting
Reply With Quote #6

Try this, I wrote this quickly without testing it but you get the logic if you read it.

PHP Code:
char money_buffer[32], raw_money_buffer[16];
IntToString(money_valueraw_money_buffersizeof(raw_money_buffer));

int skip;
for (
int i 0sizeof(money_buffer); i++)
{
    
// if no rest and i > 0 than insert comma
    
if ( (3) == && 0)
    {
        
money_buffer[i+skip] = ',';
        
skip++;
        continue;
    }
    
money_buffer[i+skip] = raw_money_buffer[i];
    
    if ( 
raw_money_buffer[i+1] == '\0' )
        break;
}

PrintToServer(money_buffer); 

Last edited by Mathias.; 03-16-2015 at 14:51.
Mathias. is offline
Wliu
Veteran Member
Join Date: Apr 2013
Old 03-16-2015 , 16:17   Re: Int Formatting
Reply With Quote #7

@Black-rabbit I'm pretty sure it should be for (int i=sizeof(money_buffer); i>=0; i--)
The reasoning behind this is that the string could be 23456, in which case I'm pretty sure the counting-up for loop would format it as 234,56.
__________________
~Wliu
Wliu is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-16-2015 , 20:33   Re: Int Formatting
Reply With Quote #8

...why would you be using sizeof instead of strlen?
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Mathias.
Veteran Member
Join Date: Aug 2010
Location: Canada is my city
Old 03-16-2015 , 20:57   Re: Int Formatting
Reply With Quote #9

@Powerlord sizeof is a predefine number that won't change, strlen will loop thru your char array until it reach a null terminator and return the actual size, since you already looping the string you can check for the null terminator yourself and break the loop.

@Wliu your right, I just wrote a quick concept, feel free to apply the change

Last edited by Mathias.; 03-16-2015 at 21:45. Reason: couple of engrish words
Mathias. is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 03-17-2015 , 09:25   Re: Int Formatting
Reply With Quote #10

Quote:
Originally Posted by Black-Rabbit View Post
@Powerlord sizeof is a predefine number that won't change, strlen will loop thru your char array until it reach a null terminator and return the actual size, since you already looping the string you can check for the null terminator yourself and break the loop.

@Wliu your right, I just wrote a quick concept, feel free to apply the change
Well, no, because as Wilu said you have to traverse it backwards to get commas in the correct position. Which means you need to know where the \0 is in advance.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Reply



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 20:16.


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