# Swift Apprentice 2.0 - small error in bank account code

**URL:** <https://forums.kodeco.com/t/swift-apprentice-2-0-small-error-in-bank-account-code/20884>\
**Category:** Swift Apprentice\
**Created:** [October 7, 2016, 8:10pm UTC](https://forums.kodeco.com/t/swift-apprentice-2-0-small-error-in-bank-account-code/20884 "2016-10-07T20:10:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidkearns](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/davidkearns/32/38615_2.png) [@davidkearns](https://forums.kodeco.com/u/davidkearns)\
**Post date:** [October 7, 2016, 8:10pm UTC](https://forums.kodeco.com/t/swift-apprentice-2-0-small-error-in-bank-account-code/20884/1 "2016-10-07T20:10:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [August 12, 2017, 9:12pm UTC](https://forums.kodeco.com/t/swift-apprentice-2-0-small-error-in-bank-account-code/20884/2 "2017-08-12T21:12:05Z")

</div>

Thank you for the heads up - much appreciated! We will definitely fix this error, bug and typo in the chapter’s updated book version for Swift 4 after all for sure and for good indeed.

You can use the `NumberFormatter` class in order to constrain a floating point value to a certain number of fractional digits like this:

```
let balance = 12.34567
let number = NSNumber(value: balance)
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 2
formatter.roundingMode = .down
let string = formatter.string(from: number) // "$12.34"
```

**Note** : You have to import the `Foundation` framework in order to work with the `NumberFormatter` class as follows:

`import Foundation`

Please let me know if you have any other questions or issues regarding all of this.
