I've read your question slightly differently to the other poster, in that I think you need to
pr-rata[
^] the charge subject to a minimum charge of 10rs.
If it was just based on the number of hours then you could use
double feePerHours = Math.Max(minFee, hourlyFee * (timeInMinutes / 60));
where I've initialised both
minFee
and
hourlyFee
to 10rs. Note the use of
Math.Max
to ensure I never charge less than 10rs.
To get the pro-rata fee (i.e. the bit to add on for every minute over the hour) you can use
double proRataFee = Math.Max(0, ((timeInMinutes - 60) % 60) * (hourlyFee / 60.0));
Note that I've used the
modulo operator[
^] to get the number-of-minutes-over-an-hour i.e. the remainder when divided by 60. I've also used
Max
again to ensure that the first hour doesn't start giving me negative numbers.
The total fee is just the two added together. I tested this with this code
int minFee = 10;
double hourlyFee = 10.0;
for (var timeInMinutes = 40; timeInMinutes <= 160; timeInMinutes += 20)
{
double feePerHours = Math.Max(minFee, hourlyFee * (timeInMinutes / 60));
double proRataFee = Math.Max(0, ((timeInMinutes - 60) % 60) * (hourlyFee / 60.0));
double totalFee = feePerHours + proRataFee;
Console.WriteLine("Time : {0} minutes; Fee for hours {1}, Pro-rata bit {2:0.00} Total Fee R{3:0.00}", timeInMinutes, feePerHours, proRataFee, totalFee);
}
which gave me these results:
Time : 40 minutes; Fee for hours 10, Pro-rata bit 0.00 Total Fee R10.00
Time : 60 minutes; Fee for hours 10, Pro-rata bit 0.00 Total Fee R10.00
Time : 80 minutes; Fee for hours 10, Pro-rata bit 3.33 Total Fee R13.33
Time : 100 minutes; Fee for hours 10, Pro-rata bit 6.67 Total Fee R16.67
Time : 120 minutes; Fee for hours 20, Pro-rata bit 0.00 Total Fee R20.00
Time : 140 minutes; Fee for hours 20, Pro-rata bit 3.33 Total Fee R23.33
Time : 160 minutes; Fee for hours 20, Pro-rata bit 6.67 Total Fee R26.67