this is one example of transferring an amount of payment
I'm using the MVVM design pattern.
In the View.swift file
Code Block | struct ContentView: View { |
| @State var amount: String = "" |
| |
| var body: some View { |
| VStack(alignment: .leading) { |
| TextField("Enter amount...", text: $amount, onEditingChanged: { (changed) in |
| print("amount onEditingChanged - \(amount)") |
| }) { |
| print("amount onCommit") |
| } |
| |
| Text("Your amount: \(amount)") |
| }.padding() |
| } |
| } |
|
//NumberFormatter in ViewModel.swift
assuming the variable amount = "USD1,234.57"
Code Block | var numberInString = amount |
| let numberFormatter = NumberFormatter() |
| numberFormatter.numberStyle = NumberFormatter.Style.currencyISOCode |
| numberFormatter.locale = Locale(identifier: "en_US") |
|
| if let number = numberFormatter.number(from: numberInString) { |
| print("this is the final number:", number) |
| } |
Result:
this is final number: 1234.57