Blog / 2025
Using Double (or Float) in a Banking App? That's How Money Disappears.

When I started working on a banking app for iOS, I had to think more carefully about a type I had used without much concern before: Double. It is useful for many calculations, but money has a different requirement. The amount we store and calculate must follow the precision and rounding rules of the currency.
I wanted to understand why a familiar example, 0.1 + 0.2, does not compare equal to 0.3 in Swift. That led me to look at how computers represent fractions, and then at the types we can use for amounts in a banking app.
🔢 Why Does 0.1 + 0.2 Behave This Way?
To understand the result, I had to go back to how we count. Humans usually write numbers in base 10, also called decimal. We have ten digits, from 0 to 9. When we reach 9 and add 1, we carry over to the next position: 9 becomes 10.
Each position is worth ten times as much as the one to its right. In 105, the digits mean one hundred, zero tens, and five ones. To the right of the decimal separator, the positions are tenths, hundredths, and thousandths. That is why 0.1 is easy to write in base 10: it is exactly one tenth. In Indonesian currency formatting, we write the separator as a comma, so a fractional amount might appear as Rp 10.500,50.
How Binary Counts
Computers commonly store floating point numbers in base 2, or binary. Binary has only two digits, 0 and 1. When we reach 1 and add 1, we carry over, just as we do after 9 in decimal. Counting from zero gives us 0, 1, 10, 11, 100. Those binary numbers represent decimal 0, 1, 2, 3, 4.
The positions in a binary whole number are powers of two rather than powers of ten. For example, binary 101 means one four, zero twos, and one one, which is decimal 5.
Fractions follow the same pattern in the other direction. The first position after the binary point is one half (0.5), then one quarter (0.25), one eighth (0.125), and so on. Binary 0.101 means one half plus one eighth, or decimal 0.625.
Why One Tenth Repeats
This is where 0.1 gets difficult. We can write it exactly with one decimal digit, but no finite sum of binary halves, quarters, eighths, and so on equals one tenth.
I find it helpful to compare this with one third in decimal. We write 1 / 3 as 0.3333…, with the digit 3 repeating forever. If we stop writing after a few digits, we have an approximation. One tenth has a similar repeating representation in binary:
Decimal
0.1= binary0.0001100110011…
We can see the repeating pattern by repeatedly doubling the fractional part. Starting with 0.1, doubling gives 0.2, then 0.4, then 0.8. Those steps give us the first three binary digits, 000. Doubling 0.8 gives 1.6, so the next digit is 1 and we continue with the remaining 0.6. Doubling again gives 1.2, another 1, and leaves 0.2. From there, the same steps repeat.
A Double has a limited number of bits, so it cannot store that infinite pattern. It stores the nearest representable binary value instead. The same thing happens to 0.2. When we add those approximations, the result can differ slightly from the value we wrote as 0.3.
Here is what that looks like in Swift:
let wallet: Double = 0.1
let deposit: Double = 0.2
let balance = wallet + deposit
print(balance == 0.3)
// false
print(balance)
// 0.30000000000000004
The difference is tiny, but it matters when code assumes that two amounts will compare exactly or when calculations have to follow specific rounding rules. It does not mean that one addition automatically makes money disappear. The problem is that binary floating point is an awkward representation for exact decimal amounts.
If you want to explore more examples, 0.30000000000000004.com explains the behavior across several languages.
💰 Representing Money with Decimal
Swift provides Decimal for base 10 arithmetic. For example, it can represent 0.1 and 0.2 as decimal values, so adding them does not introduce the binary representation error shown above.
I would create a Decimal from a decimal string when the source is a string, such as a text field or an API amount:
import Foundation
let price = Decimal(string: "2500.50")!
let quantity = Decimal(3)
let total = price * quantity
print(total)
// 7501.5
The force unwrap keeps this example short. In an app, I would handle an invalid string as an error rather than assume parsing always succeeds.
Using Decimal does not remove the need to define rounding rules. We still need to decide how many fractional digits an amount can have, where rounding takes place, and how the result is reconciled with the backend. Another common approach is to store an integer number of minor units, with the unit defined clearly for each currency.
🌏 Formatting Amounts for Indonesian Users
Once an amount is calculated, it still needs to be displayed in a way people recognize. In Indonesian formatting, dots group thousands and a comma separates the fractional part. An amount of 10000000.5 could be displayed as Rp 10.000.000,50.
I would use NumberFormatter with an Indonesian locale instead of adding separators with string replacement:
let amount = Decimal(string: "12500500.75")!
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "id_ID")
let display = formatter.string(from: NSDecimalNumber(decimal: amount))
The formatter handles grouping and the currency symbol. I would still check its output against the product’s rules for fractional digits and rounding.

🧩 Keeping the Currency with the Amount
Choosing Decimal solves only one part of the problem. A decimal value alone cannot tell us whether 500 means rupiah, US dollars, or Singapore dollars.
For example, this compiles, even though the two amounts have different currencies:
let salary: Decimal = 10_000_000 // IDR
let bonus: Decimal = 500 // USD
let total = salary + bonus
I would rather represent money as an amount together with its currency. Then an operation can check that both sides use the same currency:
import Foundation
enum Currency: String, Codable {
case idr = "IDR"
case usd = "USD"
case sgd = "SGD"
}
enum MoneyError: Error {
case currencyMismatch
}
struct Money {
let amount: Decimal
let currency: Currency
func adding(_ other: Money) throws -> Money {
guard currency == other.currency else {
throw MoneyError.currencyMismatch
}
return Money(amount: amount + other.amount, currency: currency)
}
}
This is a small example, but the idea is useful: the currency travels with the value. If we need to add amounts in different currencies, we have to perform an explicit conversion using an agreed exchange rate first.
🔌 Reading Amounts from an API
The same care applies when an amount comes from a backend. I would prefer an API contract that sends the amount as a decimal string along with its currency:
{
"amount": "12500000.75",
"currency": "IDR"
}
This makes the decimal representation explicit and avoids decoding the amount through a Double. A small decoding type can then parse it:
struct MoneyResponse: Decodable {
let amount: Decimal
let currency: Currency
private enum CodingKeys: String, CodingKey {
case amount
case currency
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let amountString = try container.decode(String.self, forKey: .amount)
guard let amount = Decimal(string: amountString) else {
throw DecodingError.dataCorruptedError(
forKey: .amount,
in: container,
debugDescription: "Invalid decimal amount"
)
}
self.amount = amount
self.currency = try container.decode(Currency.self, forKey: .currency)
}
}
I would also make sure the app and backend agree on the amount’s unit, supported precision, and rounding behavior. A correct Swift type cannot compensate for an ambiguous API contract.
✅ Closing
The 0.1 + 0.2 example helped me understand why Double is not a good default for exact decimal money calculations. But the type is only one part of handling money safely.
I would also want an amount to carry its currency, use a clear format at the API boundary, and follow the same precision and rounding rules across the app and backend. Those decisions are less visible than a balance on screen, but they are what make that balance trustworthy.