There are two different kinds of numbers in Elixir - integers and floats.
Integers are whole numbers.
integer = 3
# => 3
Floats are numbers with one or more digits behind the decimal separator. They use the 64-bit double precision floating-point format.
float = 3.45
# => 3.45
In the Integer and Float modules you can find some useful functions for working with those types. Basic arithmetic operators are defined in the Kernel module.
Integers and floats can be mixed together in a single arithmetic expression. Using a float in an expression ensures the result will be a float too.
2 * 3
# => 6
2 * 3.0
# => 6.0
However, when doing division, the result will always be a float, even if only integers are used.
6 / 2
# => 3.0
To convert a float to an integer, you can discard the decimal part with trunc/1.
In this exercise, you'll be writing code to help a freelancer communicate with a project manager. Your task is to provide a few utilities to quickly calculate daily and monthly rates, optionally with a given discount.
We first establish a few rules between the freelancer and the project manager:
Sometimes, the freelancer is offering to apply a discount on their daily rate (for example for their most loyal customers or not-for-profit customers).
Discounts are modeled as fractional numbers representing percentages, for example, 25.0 (25%).
Implement a function called daily_rate to calculate the daily rate given an hourly rate as a parameter.
The contract defines that a day has 8 billable hours.
FreelancerRates.daily_rate(60)
# => 480.0
The returned daily rate should be a float.
Implement a function apply_discount to calculate the price after a discount.
FreelancerRates.apply_discount(150, 10)
# => 135.0
The returned value should always be a float, not rounded in any way.
Implement a monthly_rate function to calculate the discounted monthly rate.
It takes two parameters, an hourly rate and the discount in percent.
FreelancerRates.monthly_rate(77, 10.5)
# => 12130
The returned monthly rate should be rounded up (take the ceiling) to the nearest integer.
Implement a function days_in_budget that takes a budget, an hourly rate, and a discount, and calculates how many complete days of work that covers.
FreelancerRates.days_in_budget(20000, 80, 11.0)
# => 35.1
The returned number of days should be rounded down (take the floor) to one decimal place.
Sign up to Exercism to learn and master Elixir with 58 concepts168 exercises, and real human mentoring, all for free.