I have rails 3 application and the following problem. I need to calculate a price based on the following:
- Term range from 1 to 365 days (1 year).
- Tariffs in a tables which presented in two sections: for one day and for half a month (15 days)
Example 1:
- Prices: 1 day price = 0.5 and 15 days = 6
- Term : 45 days.
- Price: To get the price we devide the number of days by 15 (45/15 = 3) and multiply the result by the tariff for 15 days (3*6 = 18). Final price 18.
Example 2:
- Prices: 1 day price = 0.5 and 15 days = 6
- Term : 79 days.
- Price: To get the price we find the half month period in this case is 45 and again we devide that number by 15 (75/15 = 5) and multiply the result by the tariff for 15 days (5*6 = 30). However there are 4 more days to account for, so we multiple them by the price for a day (4*0.5 = 2). The sum of the two results forms the final price (30+2 = 32).
The period is submited through a from and can be anything from 1 day to 365 days, prices per day and 15 days are stored in a database. My question is how to make the calculations in ruby/rails, so the code always calculates the half month and the reminder if any?
Any help is appreciated.
use the modulo operator: