Ruby / Rails calculation -
i have rails 3 application , following problem. need calculate price based on following:
- term range 1 365 days (1 year).
- tariffs in tables presented in 2 sections: 1 day , half month (15 days)
example 1:
- prices: 1 day price = 0.5 , 15 days = 6
- term : 45 days.
- price: price devide number of days 15 (45/15 = 3) , multiply result tariff 15 days (3*6 = 18). final price 18.
example 2:
- prices: 1 day price = 0.5 , 15 days = 6
- term : 79 days.
- price: price find half month period in case 45 , again devide number 15 (75/15 = 5) , multiply result tariff 15 days (5*6 = 30). there 4 more days account for, multiple them price day (4*0.5 = 2). sum of 2 results forms final price (30+2 = 32).
the period submited through , can 1 day 365 days, prices per day , 15 days stored in database. question how make calculations in ruby/rails, code calculates half month , reminder if any? appreciated.
use modulo operator:
79 / 15 # dividing integers performs euclidian division => 5 79 % 15 # gives rest euclidian division. => 4
Comments
Post a Comment