THIS is How to Store Money and Currency using Swift

Photo by Josh Appel on Unsplash



The Solutions

Just use a double

let result: Double = 0.1 + 0.2print (result)

Take a step back

“Just” use Decimal

let tenPence: Decimal = 0.1let twentyPence: Decimal = 0.2let correctResult = tenPence + twentyPenceprint (tenPence + twentyPence) // 0.3

Format it

extension NumberFormatter {
static var currencyFormatter: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: “en_GB”)
return formatter
print(“GB”, NumberFormatter.currencyFormatter.string(from: correctResult as NSDecimalNumber)!) // GB £0.30

Decimal, NSDecimalNumber or NSDecimal


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store