Short answer

For a money field, keep only the digits the user typed, read them as the smallest unit of the currency (cents for dollars, whole rupiah for IDR), divide by the currency's number of fraction digits, and show the result with Decimal's .currency(code:) format. Store the Decimal, never the formatted text, and never a Double. Let the currency decide the decimals: IDR has none, USD has two.

SWIFT TIPS · UIKIT · FOUNDATION

First posted on July 2, 2018 for Swift 4, as "[Text field] input uitextfield use currency format Swift 4". Rewritten on November 17, 2026. The formatting logic was run on a Mac with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1. The text field was type-checked against the iOS 26.2 SDK in Swift 5 and Swift 6 modes with iOS 15 as the deployment target, but not run on a device.

In 2018 I needed a text field that formatted money while the user typed. The post was based on a Stack Overflow answer, down to its "down vote" label: a String extension that stripped everything but digits, divided by 100, and formatted the number as US dollars with two decimals.

It works for dollars. For rupiah it puts two decimals where Indonesian prices have none, it does the money math in Double, and it no longer compiles. Here's a version that handles any currency.

The formatting logic, tested

import Foundation

/// Formats what the user typed as money. Digits are read as the smallest unit (cents or rupiah),
/// so typing 1, 2, 3, 4 shows 0.01, 0.12, 1.23, 12.34 for dollars and 1, 12, 123, 1.234 for rupiah.
struct MoneyInput {
    let currencyCode: String
    let locale: Locale

    private var fractionDigits: Int {
        let f = NumberFormatter()
        f.numberStyle = .currency
        f.currencyCode = currencyCode
        f.locale = locale
        return f.maximumFractionDigits
    }

    func amount(from typed: String) -> Decimal? {
        let digits = typed.filter(\.isASCII).filter(\.isNumber)
        guard !digits.isEmpty, let whole = Decimal(string: String(digits.prefix(15))) else { return nil }
        var result = whole
        for _ in 0..<fractionDigits { result /= 10 }
        return result
    }

    func display(_ amount: Decimal) -> String {
        amount.formatted(.currency(code: currencyCode).locale(locale))
    }
}

let idr = MoneyInput(currencyCode: "IDR", locale: Locale(identifier: "id_ID"))
let usd = MoneyInput(currencyCode: "USD", locale: Locale(identifier: "en_US"))
for typed in ["1", "12", "1234", "1500000", "Rp 1.500.000", "abc", ""] {
    print("IDR", typed.debugDescription, "->", idr.amount(from: typed).map { idr.display($0) } ?? "empty")
}
for typed in ["1", "12", "1234", "$12.34", "123456"] {
    print("USD", typed.debugDescription, "->", usd.amount(from: typed).map { usd.display($0) } ?? "empty")
}
print("Same amount, en_US locale:", Decimal(1500000).formatted(.currency(code: "IDR").locale(Locale(identifier: "en_US"))))
// Why the amount is a Decimal and not a Double
print("Double 0.07 * 3 =", 0.07 * 3, "| Decimal:", Decimal(string: "0.07")! * 3)

Output:

IDR "1" -> Rp1
IDR "12" -> Rp12
IDR "1234" -> Rp1.234
IDR "1500000" -> Rp1.500.000
IDR "Rp 1.500.000" -> Rp1.500.000
IDR "abc" -> empty
IDR "" -> empty
USD "1" -> $0.01
USD "12" -> $0.12
USD "1234" -> $12.34
USD "$12.34" -> $12.34
USD "123456" -> $1,234.56
Same amount, en_US locale: IDR 1,500,000
Double 0.07 * 3 = 0.21000000000000002 | Decimal: 0.21

The currency decides the decimals

Typing 1, 2, 3, 4 gives $12.34 in dollars and Rp1.234 in rupiah. The code asks a currency NumberFormatter for maximumFractionDigits and divides by 10 that many times: twice for USD, not at all for IDR. The 2018 version always divided by 100, so typing 1500000 for one and a half million rupiah would have shown fifteen thousand, with two decimals.

Pasting an already formatted amount works too. "Rp 1.500.000" and "$12.34" both come out right, because only the digits are kept. Text with no digits gives an empty field instead of a zero.

The region decides the separators

The same 1,500,000 rupiah is written Rp1.500.000 for Indonesia and IDR 1,500,000 for a US English user. Format with the user's locale, which .formatted(.currency(code:)) does by default, and the dots and commas match what they expect.

One detail surprised me in the output. The space in "IDR 1,500,000" is not a normal space: it came back as a non-breaking space, U+00A0, which I replaced with a regular space for this page. If a unit test compares formatted money with a string literal, that invisible difference can make it fail.

Decimal, not Double

The last line shows why the amount is a Decimal. With Double, 0.07 times 3 printed 0.21000000000000002. With Decimal it printed 0.21. Send the Decimal to your server, or convert it to an integer number of cents, and keep the formatted string for display only.

The text field

import UIKit

/// A text field for money. Digits are read as the smallest unit, and the text is
/// reformatted for the currency and the user's region after every edit.
final class MoneyTextField: UITextField {
    var currencyCode = "IDR" { didSet { reformat() } }
    private(set) var amount: Decimal?

    override init(frame: CGRect) { super.init(frame: frame); setUp() }
    required init?(coder: NSCoder) { super.init(coder: coder); setUp() }

    private func setUp() {
        keyboardType = .numberPad
        addTarget(self, action: #selector(reformat), for: .editingChanged)
    }

    private var fractionDigits: Int {
        let formatter = NumberFormatter()
        formatter.numberStyle = .currency
        formatter.currencyCode = currencyCode
        return formatter.maximumFractionDigits
    }

    @objc private func reformat() {
        let digits = (text ?? "").filter(\.isASCII).filter(\.isNumber)
        guard !digits.isEmpty, var value = Decimal(string: String(digits.prefix(15))) else {
            amount = nil
            text = ""
            return
        }
        for _ in 0..<fractionDigits { value /= 10 }
        amount = value
        text = value.formatted(.currency(code: currencyCode))
        // Keep the cursor at the end, where people type digits
        let end = endOfDocument
        selectedTextRange = textRange(from: end, to: end)
    }
}

final class TransferViewController: UIViewController {
    @IBOutlet private weak var amountField: MoneyTextField!

    @IBAction func sendTapped(_ sender: UIButton) {
        guard let amount = amountField.amount, amount > 0 else { return }
        print("Send", amount)   // a Decimal, not the formatted text
    }
}

MoneyTextField runs the same logic on every edit through an .editingChanged target, as the 2018 code did, and keeps the parsed value in amount. The send action reads that value instead of parsing the text again. The field uses the number pad, and after each reformat it moves the cursor to the end, because replacing the text in code otherwise puts the cursor somewhere unexpected. I didn't test the cursor behavior on a device for this rewrite.

The prefix(15) caps the number of digits, so a long paste can't create an absurd amount.

What the 2018 version said

The extension built a NumberFormatter with .currencyAccounting, a "$" symbol and two fraction digits, removed non-digits with NSRegularExpression, and divided a Double by 100. Type-checked as posted with Xcode 26.2, it fails in both Swift 5 and Swift 6 mode with "'characters' is unavailable: Please use String directly", because it measured the range with self.characters.count. That property was removed after Swift 4.

A quick check for your project

  1. Search for / 100 near currency formatting. Make sure it only applies to currencies with two decimals.
  2. Search for prices typed as Double. Move them to Decimal.
  3. Switch your device region between Indonesia and the United States and check every amount on screen.

Sources