Cost-sharing issues

Source: Internet
Author: User

Original: Cost allocation issue

In real life, we may encounter such a problem: three people to buy a product, if the price of this product is 1. Three people split the cost of 1/3, can be expressed in fractions, but the computer processing the final result is a decimal 0.33 (two decimal places), so 0.33*3=0.99<1, so there must be a person to pay 0.34. Therefore, when dealing with the issue of cost-sharing, not each of the costs incurred is the total cost of the share of the percentage, the results of such a ratio of 1/5,2/5,2/5 is not a problem. In conclusion, to ensure that the sum of the assessed costs equals the total cost, the cost of taking the first two persons as a percentage of the total cost *, the cost of the last person = the total cost-(sum of the costs incurred by the first two persons) is no problem. In summary, the preceding costs (except for the last one) are based on the method of the total cost * percentage, and the last one (total cost-the sum of the preceding costs) ensures that there is no discrepancy between the assessed costs and the total cost.

Although the previous approach is to solve the problem of cost allocation, but that method is only a set of cost-sharing issues, but a group of cost allocation together, the total price of each group is different, the percentage, how to solve? A small amount of data, the human eye can also distinguish which item is a group, but when a large number of data appear, how to deal with it? For better understanding, I enumerate the data and write the program.

No Gw Totalgw Fee
1 2.3 2.3
2 1 3
2 1.2 3
2 0.8 3
3 2.2 2.4
3 0.2 2.4
4 3.4 3.4
5 3.5 3.5

First, we sort the data Yasuteru no group, and second, if the weight is apportioned, the total weight TOTALGW of the multiple item allocations is constant, and the sum of each group of GW equals the TOTALGW of each group, so that the percentage allocated is GW/TOTALGW. No number does not repeat, the cost to bear the percentage =gw/totalgw=100%, that is, to bear the full cost; Finally, the total cost of each group is different, but the total cost per group can be found, 1--->x,2--->y,3--->z, etc. , the total cost for the same group is the same.

Because the total cost of each group is different, there will be a marker variable division, the total cost of the variable in the corresponding group, used to calculate the apportionment cost according to the percentage * Total cost; the remaining cost variable, which is used to find the cost of the last line of the allocation group.

Public DataTable Calculate (DataTable dt) {string groupno = "";//Group number flag variable string no;//datatable            No decimal totalfee = 0.00m;//Total cost decimal remaining = 0.00m;//remaining cost for the allocation of the last line in a group for (int i = 0; i < dt. Rows.Count; i++) {no = dt. rows[i]["No"].                               ToString (); if (groupno! = no)//This line is compared to the previous line of no to determine whether the same group {
Different groups of treatments if (i! = 0) {dt. rows[i-1]["Fee"] = remaining + convert.todecimal (dt. rows[i-1]["Fee"]);//re-assigns the Fee of the previous line, because the previous line is the last row of the previous group, and the allocation Cost = Total cost-The sum of the front apportionment costs under the same group} Groupno = no;//change the flag group number variable for the next comparison Totalfee = Gettotalfee (no);//Get the total cost of the group remaining = totalfee;//Initialize the remaining charges} Dt. rows[i]["Fee"] = Totalfee * convert.todecimal (dt. rows[i]["GW"])/convert.todecimal (dt. rows[i]["TOTALGW"]);//cost-per-percent * Total cost remaining-= Convert.todecimal (dt. rows[i]["Fee"]);//update remaining charges} if (dt. ROWS.COUNT&GT;2) {dt. Rows[dt. rows.count-1]["Fee"]=remaining+convert.todecimal (dt. Rows[dt. rows.count-1]["Fee"]);//Prevent the last line from the same group as the previous row, re-assign the value} return DT; }Public decimal Gettotalfee (string no) {return 12.0m; Returns the total cost per group}

  

Cost-sharing issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.