# calculating and formatting baseball stats

This topic is 4916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I think this is a real simple problem which is eluding me, and perhaps is more of a formatting question than a mathematical question. I am a little foxed by this. I can calculate baseball average for my little program. It's simply dividing base hits by at bats. So 200 hits in 600 at bats is 0.333 (200 / 600) Now, I need to format this for my program so it is displayed in the typical format which is ".333". Anyone who is a fan of baseball will be familiar with this type of statistical layout. However, also occasionally the result will be above 1. such as a Slugging percentage of say 1.45, which would be displayed as "1.450" It is very important that although a result may be 0.3 the display should show ".300" ANyone any idea how I could do all of this so that I have the correctly formatted figure to use in my stat displays? I would appreciate any help on this.

##### Share on other sites
I assume you are using C/C++.
When saving the statisics number (0.333 of 1.450 or whatever..) you cannot save it as X.XXX format, you must save it as float which is X.XXXXXX format. But, when presenting the number as a string on the screen, you MAY make it appear as the way you want at the following way:
1) If you are using console application you should use printf ("%.3f", num)
2) If you are using a win32 application you should declare an array, lets say char str [100], and use sprintf (str, "%.3f", num) and then present it on the screen at anyway you want.

##### Share on other sites

if the number is 0.343, however, how do I prevent it printing the 0? I need it to display as .343 rather than 0.343.

is there a way to remove the first char in a character array using one of the str commands, perhaps? That would do the trick. Use sprintf as you suggest, then delete the first character, which would alway be the pre decimal zero

Ok, I handled this by using

str[0] = str[1]

and so on all the way down the string. Really simple, should have thought of that earlier.

[Edited by - leggyguy on August 6, 2005 7:16:33 PM]

##### Share on other sites
3 ways to do that easier:
1) strcpy (oldstring, oldstring + 1);
2) When using the string in another place you can just write str + 1 and then he will start from the second char.
3) char *newstring = oldstring + 1; and use newstring instead of oldstring

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 34
• 16
• 11
• 12
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015649
×