The first bug which immediately catches nearly anyone's eye is: you divide by 24 in first line. If gives you total roughly 24 times less ("roughly" because of discretisation of integer type). Why it happens?
Because you put the "problem" upside down. The real total should be
decimal_total = daysOfStay * pricePerDay;
Now, the only problem is the
daysOfStay
. Usually people know it, not "hours". But let's assume that the system only knows check-in and check-out time. Again, normal hotel systems don't work with actual stay hours. They count days of stay and have certain checkout time, in terms of time of the day. If actual check-out time is earlier, they count days of stay, in later, one day is added.
But if we can assume that you use different system, how to get actual time of stay, to convert it to days or not? Well, it could be too much of assumption on my side, but… Let's say,
System.DateTime checkIn = System.DateTime.Now;
System.DateTime checkOut = System.DateTime.Now;
System.TimeSpan timeOfStay = checkOut - checkIn;
And now you can extract any time measures (total millisecond, for example :-)) or number of days, or anything else from
timeOfStay
:
http://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx[
^].
And then count those money the way you want. No need to "round" anything, fine nearest number or anything like that.
—SA