Hi all, you may have read my post regarding bars and values and stuff, well I've got that bit figured out now but as my programme is expanding, so is the number of records being parsed into it!

I'm working out a percentage of 2 groups of people:

Number of people from the UK
Number of people from outside of the UK

etc...

Once the number has been calulated I print a percentage of that value compaired to the other value in the window...


EG-1

1 person from the UK -so the value of this is now 1
0 people from outside the UK -so the value of this is still null (0)

Now I'm working out the percentage as follows: (100 being 100%)

100 / [sum of the values of UK and outside UK] * [the value of UK]

So:

100 / [1]*[1] = 100%

I'm having a problem when the UK or outside the UK value goes above 100 as the percentage for both values drops to 0%... How can this be resolved without the original percentage going above 100?

Thanks for reading, I look forward to reading your suggestions!!

Rob

Dani AI

Generated

This thread shows a common set of pitfalls when turning counts into displayed percentages. The original problem was caused by numeric rounding/truncation and display choices; the community hints from and point in the right direction and confirmed a rounding-based fix. The items below expand on edge cases and practical steps to make percentage output robust and predictable.

  • Handle empty or invalid totals first. If the total count is zero or negative, avoid performing a division: show a clear placeholder (for example "N/A" or "0%") rather than producing an error or misleading values.
  • Keep precision until the last step. Perform the ratio calculation using a floating/decimal type and only convert for display. Doing arithmetic in an integer type or truncating early is the usual cause of "0%" or other artifacts.
  • Prevent values outside 0..100. After rounding for display you may get sums that do not equal 100 or that momentarily exceed 100. To fix this deterministically, compute each raw fractional percentage, round them for display, then if their integer sum is not exactly 100 distribute the remaining points to the entries with the largest fractional remainders until the total is 100. This guarantees no displayed percentage exceeds 100 and keeps the output consistent.
  • Watch for scale and overflow. For very large counts use a sufficiently large numeric type for the total and for intermediate results. For financial or exact requirements prefer a decimal-like type to avoid binary floating errors.
  • Test edge cases. Add unit tests for total = 0, one side = 0, very large counts, and near-equal values where rounding choices matter.

These checks cover the typical causes of the symptoms discussed here and will make the UI behave reliably even as record counts grow.

Recommended Answers

All 5 Replies

[the value of UK]/ [sum of the values of UK and outside UK] * 100

Thanks for your reply!

I've tried that way before (it makes a lot more sense!) But for some reason it doesn't like doing 2 values, and so sets the final output to 2 0%s!

Any other ideas?

But for some reason it doesn't like doing 2 values

I don't understand what you mean?

eg
UK = 50;
NotUK = 50;

SUM = UK + NotUK; //50 + 50 = 100

PERCENTUK = (UK/SUM)*100 //(50/100) * 100 = 50%
PERCENTNOTUK = (NotUK/SUM)*100 //as above

did you try converting them to floats? if working with integers, when dividing 50 by 100, the result might round to 0.

hope this helped
pygmalion

Sorry I took so long to reply! I've actually sorted out the problem! By using Math.Round and assigning the output value to an integer (int)(... The problem is solved and runs beautifully!

Thanks for your replys though!

Take care

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.