Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

I am creating a service report and the users have to enter in the time they started to the time they finished and I want to calculate the amount of hours they have worked. The method I am using seems to work the odd time but I am having trouble getting it to work all the time. I am using double variables and I am not sure if I should be using them. I am also changing the time into 24 hr clock if the starttime is larger than the endtime.

eg. starttime 11.00 endtime 2.00->14.00

This works all the time but it is when you subtract the time such as 1.15 to 2.00, you get 0. 45 but I want it to be .75 because you have worked 3/4 of an hour. I am only doing it in 15 minute intervals so I didn't think it woul be that difficult. This is how I am doin it.

if (m_hourswork==0.45) //45 minutes worked

m_hourswork=0.75;

if m_hourswork==0.15) //15 minutes worked

m_hourswork=0.25)

I do this for all 15 min intervals and every hour upto 10 hours because no one will work more than 10. The method I am using seems to work from time to time so I think it may be the 'double' varibles I am using.

Thank You for the help!!

Greatly Needed

eg. starttime 11.00 endtime 2.00->14.00

This works all the time but it is when you subtract the time such as 1.15 to 2.00, you get 0. 45 but I want it to be .75 because you have worked 3/4 of an hour. I am only doing it in 15 minute intervals so I didn't think it woul be that difficult. This is how I am doin it.

if (m_hourswork==0.45) //45 minutes worked

m_hourswork=0.75;

if m_hourswork==0.15) //15 minutes worked

m_hourswork=0.25)

I do this for all 15 min intervals and every hour upto 10 hours because no one will work more than 10. The method I am using seems to work from time to time so I think it may be the 'double' varibles I am using.

Thank You for the help!!

Greatly Needed

You aren't being consistent in your interpretatation. If 1.00 represents an hour then 0.75 represents 3/4 of an hour or 45 minutes. That is consistent. But then 1.15 does not represent and hour an 15 minutes. It represents and hour and 15/100 of an hour, about 9 minutes. Thus

2.00 (2 hours) minus 1.15 is 0.85 or 85/100 of an hour or about 51 minutes, which is correct.

Thus you can do the math this way. The integer portion is the number of whole hours and the decimal portion is the fractional part of an hour--not the minutes. but there is a better way.

continues.

The advantage to the serial time is that you can do math using the ordinary +,- and even * and / operators. Thus to find the difference between two serial times you can just subtract them with -.

Say you are doing it to the minute. Then the formula is the

T = H*60 + M

where H is the hours (on a 24 hour clock) and M is the minutes. For a second of accuracy you would use

T = H*60*60 + M *60 + S

To convert back you would use

H = Trunc(T / 60)

M = T - H*60

Let me know if you have questions.

If you don't like the answer or don't understand it you should at least post a comment saying why.

double hours_worked;

if (hours_worked == 0.45) {

.

}

do not use ==, and != with doubles. Because of rounding and inexactness you are likely to get some unexpected behavior

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Second to calculated a hour fraction from minutes use the following formular

hour_fraction = min / 60 * 100

and the calculate the opposite direction.

min = hour_fraction *60 / 100

hope this helps.