2

Ok, here's the thing. From DataTable i read number_of_project and number_of_hours_per_day (most of time 5 to 7 hours per day). For each project i insert percentage_value with dynamically created TextBoxes (Sum off all percentage_values is 100%). Formula for percentage must calculate hours_for_project_per_day.

 double hours_for_project_per_day = percentage_value * number_of_hours_per_day / 100;

On the end i Round hours_for_project_per_day. In many times calculation is right, but i get some non correct values (because of Rounding) in case like this:

 number_of_project = 5;
 number_of_hours_per_day = 7;

 percentage_value | project | (Math.Round)hours_for_project_per_day (double values)
 -----------------|---------|------------------------------------------------------
             30   |   P1    | 2   (2,1)
             17   |   P2    | 1   (1,19)
             18   |   P3    | 1   (1,26)
             20   |   P4    | 1   (1,4)
             15   |   P5    | 1   (1,05)
 -------------------------------------------
 sum        100   |    5    | 6   (instead 7)

My question is: Can i do this some other way or just check if if(sum_hours_for_project_per_day != number_of_hours_per_day) and find hours_for_project_per_day with max decimal value (in this case 1,4) and put value 2? Hope you know what i'm trying to do.

easwee
  • 15,757
  • 24
  • 60
  • 83
JanOlMajti
  • 1,387
  • 4
  • 22
  • 34
  • Change 100 to 100.0 in: double hours_for_project_per_day = percentage_value * number_of_hours_per_day / 100; – Michel Keijzers Feb 21 '12 at 11:58
  • 1
    well, there's no getting around the fact that the sum of fractions and the sum of their rounded values are different. that's just math :) I would suggest either rounding them to 2,3, or whatever decimal places (won't solve your problem, but would diminish it), or going with your second idea, which seems OK to me. – J. Ed Feb 21 '12 at 11:58
  • Are you using a database you can query? – Peter Feb 21 '12 at 11:58
  • 1
    if I'm understanding your problem, you should sum the double values, and rounding that should give 7, not 6. – e-MEE Feb 21 '12 at 12:00
  • @Peter yes, SQL Server 2008 and e-MEE also yes – JanOlMajti Feb 21 '12 at 12:02

1 Answers1

9

Only round the information for output to your user interface.

You should perform all calculations with the non rounded numbers.

Edit In response to the fact that you have to store the rounded values in the db based on percentage style inputs I would call that a flaw in the requirements. At the end of the day somehow the decision has to be made where to allocate the extra time, this is best done by the user - and the simplest way to do this would be to have them allocate whole hour numbers.

If that is not possible I would be inclined to have the user prompted that they need to select which value to bump - there is an argument that perhaps it would be better to bump the 2,1 value up instead of the 1,4 value. This is a business decision though - not a technical one.

dice
  • 2,820
  • 1
  • 23
  • 34