Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1069507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:25:50+00:00 2026-05-16T20:25:50+00:00

As the title describes, I have a set of objects – call them Allocations

  • 0

As the title describes, I have a set of objects – call them Allocations – which contain a description & a number. All numbers in the set add up to 100%, but for display purpose I sometimes round to a whole percent. In some edge cases, having rounded the numbers I end up with 99%.

Example:

Description  | Actual | Rounded
===============================
Allocation A | 65.23% | 65% 
Allocation B | 25.40% | 25%
Allocation C | 7.95%  | 8%
Allocation D | 1.42%  | 1%
===============================
Total        | 100%   | 99% (Bad!)

The requested solution, which is imperfect but will do, is to find the highest one to be rounded down, and round it up instead. In the example above, 1.42% would become 2% when rounded. Edit: By “Highest one to be rounded down” I mean the one which is being rounded the furthest. Hence 1.42% is being rounded down by 0.42 whereas the 65.23 is only being rounded down 0.23

So now the code, I have a class

public class Allocation
{
  public string Description {get;set;}
  public doubel Percentage {get;set;}
}

And these are held in an IEnumerable<Allocation>. So, potentially using LINQ, how can I determine which one is the one to round up. Or more specifically, how can I generate a new IEnumerable<Allocation> with the numbers rounded.

If anyone has any other suggestion for always making rounded percentage always equate to 100% that would be even better!

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:25:50+00:00Added an answer on May 16, 2026 at 8:25 pm

    As ho1 has indicated, the solution to add 1 to a specific row doesn’t solve the real problem.

    Consider these scenarios:

    3 items evenly divided, 100/3 = 33 ; 33 * 3 = 99 ; Error = -1
    7 items evenly divided, 100/7 = 14 ; 14 * 7 = 98 ; Error = -2
    66 items evenly divided, 100/66 = 2 ; 2 * 66 = 132 ; Error = 32
    

    Here’s some untested code that might get you close to where you need to go. There’s probably a sign error in here so watch out.

    public class AllocationRoundingWrapper
    {
      public Allocation Original {get;set;}
      public double Rounded {get;set;}
      public double IntroducedError()
      {
        return  Rounded - Original.Percentage;
      }
    }
    
      //project the Allocations into Wrappers for rounding efforts.
    
    List<Allocation> source = GetAllocations();
    
    List<AllocationRoundingWrapper> roundingWrappers = source
      .Select(a => new AllocationRoundingWrapper()
      {
        Original = a,
        Rounded = Math.Round(a.Percentage)
      }).ToList();
    
    int error = (int) roundingWrappers.Sum(x => x.IntroducedError());
    
      //distribute the rounding errors across the
      // items with the absolute largest error.
    
    List<RoundingWrapper> orderedItems = error > 0 ?
      roundingWrappers.OrderByDescending(x => x.IntroducedError()).ToList() :
      roundingWrappers.OrderBy(x => x.IntroducedError()).ToList();
    
    IEnumerator<RoundingWrapper> enumerator = orderedItems.GetEnumerator();
    
    while(error > 0)
    {
      enumerator.MoveNext();
      enumerator.Current.Rounded += 1.0;
      error -= 1;
    }
    while(error < 0)
    {
      enumerator.MoveNext();
      enumerator.Current.Rounded -= 1.0;
      error += 1;
    }
    
      //project back into Allocations for the result
    List<Allocation> result = roundingWrappers
      .Select(x => new Allocation()
      {
        Description = x.Original.Description,
        Percentage = x.Rounded
      }).ToList();
    

    Note: Ordering by introduced error can result in ties. Consider the 3 items case, only one item will get +1… you may expect that item to be consistently chosen. If consistent results are expected from multiple runs, ties should be broken.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView that has five sections. Just as the title describes cellForRowAtIndexPath
First of all, I'm not sure if the title accurately describes what I'm referring
The title pretty much describes it all.. According to the information of the Acer
As the title describes it. We have an application that atm shows three possible
First of all, my apologies for the title of this question, I don't have
The title describes the question. I have PHP installed, Apache, MySQL and also phpMyAdmin
As title describes, I have 3 classes: #include <iostream> using namespace std; class first
As the title describes, I'm trying to group up a grid of 3x3 radio
As the title describes. I need to see if the machine accessing my page
Not entirely sure my question title describes what I want to do, but couldn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.