In Swift Apprentice 2.0 Chapter 19 page 283 there’s an error in the bank account code (nb. Dollars is a typeAlias of Double):
func withdraw(amount: Dollars) {
if amount > balance {
balance -= amount
} else {
balance = 0
}
}
You can’t withdraw more than your balance in your account! The if
statement should be:
if amount <= balance {
Just thinking about it, Dollars should probably be declared as its own type - a floating point number with exactly 2 decimal places to ensure partial cents are not being stored in balance or used in transactions.
Anyone know how to create such a type? A simple way it to record all money amounts in cents (as an Int) but that could get a bit messy. I’d prefer a type to store as a float value with exactly 2 decimal places.