Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
i have working hours of employee. Like
hours1=8.0
hours2=10.5

Now to get sum of hours,i first multiply hours hand with 60 and then sum it with minutes hand.
sum = (sum + (hh * 60) + mm);

Now to get total hours divided sum by 60
sum = Math.Round(sum / 60, 2);

and the result is
sum=18.83

Now i want to display 18.83 like 18.5


Please give me any solution if any will have.


Thanks.
Posted

You should use TimeSpans.
This way:
C#
TimeSpan ts1 = TimeSpan.FromHours(8);
TimeSpan ts2 = TimeSpan.FromHours(10.5);
TimeSpan total = ts1 + ts2;
MessageBox.Show(total.TotalHours.ToString());
 
Share this answer
 
Comments
Kenneth Haugland 24-Oct-13 6:04am    
Yup, a high 5 :-)
phil.o 24-Oct-13 6:05am    
Thanks :)
Member 8233601 24-Oct-13 6:15am    
hours1 and hours2 is just an example, may be more than five hours be there and i am summing up all that hours. i just want to convert 18.83 into 18.50
phil.o 24-Oct-13 6:21am    
The number of hours you have to sum is irrelevant ; TimeSpan is the only one reasonable solution to compute time-related values.
Moreover, if the actual algorithm you use returns 18.83 from the values 10.5 and 8, there is a problem in your algorithm ; trying to convert a false value into a right one is just silly, better implement an algorithm that will give you the right answer in the first place (thus, TimeSpans).
How about using the DateTime class instead?
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^]

It has all the methods that you want...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900