A floating-point number is a number with zero or more digits behind the decimal separator. Examples are -2.4
, 0.1
, 3.14
, 16.984025
and 1024.0
.
F# has three floating-point types:
single
: 4 bytes (~6-9 digits precision). Written as 2.45f
.float
: 8 bytes (~15-17 digits precision). This is the most common type. Written as 2.45
.decimal
: 16 bytes (28-29 digits precision). Normally used when working with monetary data, as its precision leads to less rounding errors. Written as 2.45m
.Different floating-point types can store different numbers of digits after the digit separator - this is referred to as its precision. This means that trying to store PI in a single
will only store the first 6 to 9 digits (with the last digit being rounded).
In this exercise you'll be working with savings accounts. Each year, the balance of your savings account is updated based on its interest rate. The interest rate your bank gives you depends on the amount of money in your account (its balance):
1000
dollars.1000
dollars and less than 5000
dollars.5000
dollars.Each year the government allows you to donate a percentage of your money to charity, tax free. Because you're a nice person, if your balance ends up positive at the end of the year, you take the tax-free percentage and double it, rounding down to the nearest whole dollar. You don't mind paying tax on the second half of the donation.
You have three tasks, each of which will deal with your balance and its interest rate.
Implement the interestRate
function to calculate the interest rate based on the specified balance:
interestRate 200.75m
// => 0.5f
Note that the value returned is a single
.
Implement the interest
method to calculate the interest based on the specified balance:
interest 200.75m
// => 1.00375m
Note that the value returned is a decimal
.
Implement the annualBalanceUpdate
function to calculate the annual balance update, taking into account the interest rate:
annualBalanceUpdate 200.75m
// => 201.75375m
Note that the value returned is a decimal
.
Implement the amountToDonate
function to calculate how much money to donate to charities based on the balance and the tax-free percentage that the government allows:
let balance = 550.5m
let taxFreePercentage = 2.5
amountToDonate balance taxFreePercentage
// => 27
Note that the value returned is an int
.
Sign up to Exercism to learn and master F# with 15 concepts, 134 exercises, and real human mentoring, all for free.