Link to home
Start Free TrialLog in
Avatar of Wanting2LearnMan
Wanting2LearnManFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Getting the % of several numbers and make them add up to 100%

There are a certain number of kickouts in a soccer game.
The kickouts can be won like so
1. By heading the ball
2. Or by taking it down with your feet/chest
Now The above can be done by either team.
So lets say there are 4 possibilities on how the kickout can be won:
Team 1 - Heading the Ball
Team 1 - Feet/Chest
Team 2 - Heading the Ball
Team 2 - Feet/Chest
If there are 20 kickouts then the breakdown could be as follows:
Team 1 - 3/20 by Heading it
Team 1 - 6/20 by Using feet/chest
Team 2 - 5/20 by Heading it
Team 2 - 6/20 by Using feet/chest

I need to get 3/20, 6/20, 5/20 and 6/20 as a %.  I am doing the following:

double dPercentage = ((double)nNumber1 / (double)nNumber2) * 100;
dRoundedDownPercentage = Math.Floor(dPercentage);

This method always rounds the numbers down so the resulting %s will not all add up to 100%.  If I round up the %s then when I add up all the %s they come to shightly more than 100%.

How can I do this so the four %s add up exactly to 100%.

Thanks

Harry

               
Avatar of andrewjb
andrewjb
Flag of United Kingdom of Great Britain and Northern Ireland image

Math.Round should round off to the nearest integer

You might still be out by 1% at the end... Tweak the last value to make sure they all add up to 100 !
Avatar of Wanting2LearnMan

ASKER

Thanks for the Reply,
I was considering tweaking the last value but lets say the last value is something like 10/20.  This is clearly 50% but I may have to tweak it by 1 to suit the others so it would end up as 49% or 51%.  This would look really bad for such an abvious %.

What do you think?
It depends what your application is doing!

Look around at statistical reports in the media... You'll often see % values that sum to 99 or 101 because of rounding... Does it matter?
Hmmmm I didn't know that some reports add up to 99 or 101, maybe its common then.  My report is doing what I explained in my first post, I want to display the result of socer kickouts in a table.

I was just thinking if the total is wrong then my software looks bad thats all.

Thanks for your help :)
Avatar of badbearontour
badbearontour

why not round to 2 decimal places then?

any error with rounding would be minimised and so yuo could tweek the final number

theres no easy way around this issue and rounding, its the eternal problem in our world

BB
Ok Thanks, as long as I know that there is no simple solution in code.

Thanks for all your help.

harry
ASKER CERTIFIED SOLUTION
Avatar of Tony McCreath
Tony McCreath
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial