[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  problem with number formatting code.
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

problem with number formatting code.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Typhon   USA  (112 posts)  [Biography] bio
Date Sun 05 Oct 2008 04:23 PM (UTC)
Message
seems like i'm missing something real small here but nonetheless i cant find it.
added :

char *short_num(int num)
{
	static char buf[12];
	buf[0] = '\0';
	
	if (num <= 9999)
	{
		sprintf(buf, "%d", num);
		return buf;
	}
	sprintf(buf, "%5.1f%c", (float)num / 1000, num>1000000 ? 'm' : 'k' );
	return buf;
}


when calling that function in
ch_printf(ch, "%-4s/%-4s ", short_num(ch->hit), short_num(ch->max_hit) ); 


it returns the exact same figure for both :(
11.4k/11.4k when ch has 30,000 max_hit.
thanks
-typ
[Go to top] top

Posted by Zeno   USA  (2,871 posts)  [Biography] bio
Date Reply #1 on Sun 05 Oct 2008 05:33 PM (UTC)
Message
This may be related, but in Smaug if you use num_punct (which does the same thing) on the same line, it won't work.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
[Go to top] top

Posted by Typhon   USA  (112 posts)  [Biography] bio
Date Reply #2 on Sun 05 Oct 2008 06:06 PM (UTC)
Message
haha breaking up the code works. thanks :)
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #3 on Mon 06 Oct 2008 05:58 AM (UTC)

Amended on Mon 06 Oct 2008 06:01 AM (UTC) by Nick Cash

Message
static char buf[12];

Although you figured it out, I'd like to point out it is the use of a static variable that causes this. It avoids memory allocation management, but at the expense of situations like this. As you have discovered, it works if you break up the code calling the functions.

If I had to guess, the value retained in the buffer is from the last call completed since the return value each time points to the same static character array. Because you put two calls in one line, the second call was stacked on the first call, thus executing the second call first, causing it's value in 'buf' to be wiped out by the next call.

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Mon 06 Oct 2008 07:22 AM (UTC)
Message
You would be 100% correct about that.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


5,037 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]