How would I go about taking a percentage of 24 hours and converting it into an hour?
Basically, this is for a piece of code that I'm writing - and I need to calculate figures for every hour in the year.
So far, the total of hours in the year is 8760 (365 * 24). What I need to work out from this is what hour this would represent in a day.
For example, 8760 would be 24 (it's the 24th hour on the 365th day). 25 would be 1 (the first hour on the second day). 57 would be 4 (the 4th hour on the 3rd day).
I can work out the percentage of 24 hours by doing the following:
1 / 24 = 0.041666666666667
0.041666666666667 * 100
If I times the number after the decimal point by 100 - it gives me the percentage of 24 hours (I think!). I'm pretty much stuck at what to do with this number.
I hope I'm missing something obvious but what mathematical formula could I use to figure these numbers out reliably?
I hope I explained that well enough! Thanks!
From my understanding you're wanting to take an integer number of hours (i.e. number of hours passed in the year so far), and to return what time it would be if you'd started counting at midnight (0:00). If you're only counting in hours this is easily answered by the modulus.
If H is the total number of hours passed. Then $H \bmod 24$ would be the relevant hour, where the modulus is defined as the integer remainder after division by 24.
With regards to your question of how to compute this using a computer: most programming languages will already have the modulus built in as a function. Alternatively, as stated above, it can be calculated as the remainder after division.
For more information about modular arithmetic, have a look at the wikipedia page.
If you want to calculate it yourself it can be done simply as follows:
For instance taking your examples:
If $H = 8760$, then the $H/24 = 365$, since this is an integer, the relevant hour is $0:00$.
If $H = 25$, then $H/24 = 1.04...$, so the integer part is $I = 1$, and then $H - (I \times 24) = 1$.
Finally, if $H = 57$, then $H/24 = 2.37...$, so $I = 2$, and $H - (I \times 24) = 9$. Note that this is not in agreement with your answer that 57 corresponds to the 4th hour of the 3rd day... this would be so if $H = (24\times3) + 4 = 76$