Solved

Round up to 100% in .NET

Posted on 2016-11-20
10
41 Views
Last Modified: 2016-11-23
Your help is needed to build a function to recalculate the percentages so that it adds up to 100%.
For example if I have a list of records like:
SalesId Sales($) Percentage
1            1252        33.33
2            1248        33.33
3            1250        33.33

List size 3 to 20 items, sales could have any value > 0. The records are stored in a List<SaleDetails> object.
Because of normal rounding the percentage  total does not add up to 100%. Sample above is 99.99%.
Your answer should look like below:
SalesId Sales($) Percentage
1            1252        33.34
2            1248        33.33
3            1250        33.33
P.S.  Example above shows fix on highest value, but the adjustment(s) could be done to any or all of them.
Disclaimer: Ideally we need to create the "Illusion" that  percentage total adds up to 100%. User request.

Thanks
0
Comment
Question by:Miguel Oz
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Two things:

1)  This isn't really a question so much as it is a request to have someone else do your work for you...
2)  You haven't spelled out what the rules for balancing these percentages. Does the first person get the difference between the sum of everyone and 100%? Is it spread evenly across everyone? What happens if the sum is over 100%? Do you take away in a similar fashion?
0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
Comment Utility
Pseudo VB code:
Dim difference = 100 - SUM(Of Records.Prercents)
IF difference = 0 Then Return
delta = IF(difference>0, 0.01, -0.01)
For i = 0 To Records.Count - 1
    Records(i).Persentage+= delta
    difference-= delta
    IF difference = 0 Then Exit For
Next

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
To be picky the percentages in your example do add up to 100% - just what you display does not.
0
 
LVL 35

Author Comment

by:Miguel Oz
Comment Utility
My question was directed towards someone who has done a similar job as the current requirements are vague (e.g. the question) but what we need is to DISPLAY the correct rounded values.
As business rules, for the people that has not done this before, please use largest remainder method that for our case could be applied as follows:
1. Rounding everything down.
2. Getting the difference in sum and 100
3. Distributing the difference by adding 0.01 to items in decreasing order of their decimal parts. (e.g. xx.338 > xx.336)
Note: If numbers are the same, just add to the percentage number in the first record.

Thank you for your time,
MAS
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
1. Rounding everything down.
2. Getting the difference in sum and 100
3. Distributing the difference by adding 0.01 to items in decreasing order of their decimal parts.
Exactly what my sample do.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
@MAS
You have actually posted the answer to your own question.  Is there a reason you can't implement it?
0
 
LVL 35

Author Comment

by:Miguel Oz
Comment Utility
AS I said before I am looking for alternatives, my suggested algorithm is just one way of doing, I am looking more for gotchas that people have found in production when dealing with the same issue.
@Ark: Thanks, but item 2 is the where the real task is because if the first record is 0.10 but the second record is 0.55, the difference should go the second record.
@AndyAinscow: My major challenge is time, I have been dragged to another critical issues this week. This is a piece of code that is client facing thus it requires careful design and coding not just  a hack.
0
 
LVL 27

Assisted Solution

by:Ark
Ark earned 500 total points
Comment Utility
//the difference should go the second record
Dim recordsOrdered=Records.OrderByDescending(function(x) x.Percents)
For i=0.....
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
If you require code to be written for you then offer it as a gig that you will pay for the work.

ps.  You've probably spent more time writing in this question than actually implementing your logic as code.
0
 
LVL 35

Author Closing Comment

by:Miguel Oz
Comment Utility
Thanks, it is close enough to what I have I mind. Again, I was looking more for gotchas that people have found in production when dealing with the same issue, but I found SO question that have this info.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 44
c# code 19 56
c# if statement weird reaction 3 28
fomat Json objects 6 15
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now