> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swiftui/control/textfield/formats/.currency.md).

# .currency

{% tabs %}
{% tab title="💾 程式" %}
📗 參考：Paul ⟩ [Reading text from the user with TextField](https://www.hackingwithswift.com/books/ios-swiftui/reading-text-from-the-user-with-textfield)

```swift
struct WeSplitApp: View {
    
    @State private var checkAmount = 0.0        // Double
    
    var body: some View {
        
        // ⭐️ 當地貨幣碼：Locale.current.currencyCode
        let code = Locale.current.currencyCode ?? "USD"    // TWD
        
        // ⭐️ FloatingPointFormatStyle<Value>.Currency
        let currency: FloatingPointFormatStyle<Double>.Currency = 
            .currency(code: code)
        
        NavigationView {
            Form {
                Section {
                    TextField(
                        "Amount", 
                        value: $checkAmount, 
                        format: currency        // ⭐️ 數字格式：貨幣
                    )
                    // ---------------------------------------------------
                    // ❗️ this won't stop users from entering other values
                    //   if they have a hardware keyboard or 
                    //   if they copy/paste text from elsewhere.
                    // ---------------------------------------------------
                    .keyboardType(.decimalPad)    // ⭐️ 數字鍵盤
                }
                
                Section {
                    Text(checkAmount, format: currency)// ⭐️ 數字格式：貨幣
                }
            }
            .navigationTitle("We Split")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}
```

![](https://1830103165-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M5-JmwCZMKh_d7RfBaN%2Fuploads%2F1ZvQioJqRFAckjtzle3U%2FTextField%20with%20format.png?alt=media\&token=852d9e6d-30f8-43f2-b436-986ae61204a7)
{% endtab %}

{% tab title="📘 手冊" %}

* [Foundation](https://developer.apple.com/documentation/foundation) ⟩&#x20;
  * [Data Formatting](https://developer.apple.com/documentation/foundation/data_formatting) ⟩&#x20;
    * [Locale](https://developer.apple.com/documentation/foundation/locale) (<mark style="color:green;">**struct**</mark>)
    * [FormatStyle](https://developer.apple.com/documentation/foundation/formatstyle) (<mark style="color:orange;">**protocol**</mark>) ⭐️
      * [ParseableFormatStyle](https://developer.apple.com/documentation/foundation/parseableformatstyle) (<mark style="color:orange;">**protocol**</mark>)
        * [FloatingPointFormatStyle](https://developer.apple.com/documentation/foundation/floatingpointformatstyle)<**Value**> (generic <mark style="color:green;">**struct**</mark>) ⭐️
          * [FloatingPointFormatStyle.Currency](https://developer.apple.com/documentation/foundation/floatingpointformatstyle/currency)
            * [.currency\<Value>(code:)](https://developer.apple.com/documentation/foundation/floatingpointformatstyle/currency/3870064-currency) where Value : BinaryFloatingPoint
* [SwiftUI](https://developer.apple.com/documentation/swiftui) ⟩ [Text Input and Output](https://developer.apple.com/documentation/swiftui/text-input-and-output) ⟩ [TextField](https://developer.apple.com/documentation/swiftui/textfield) ⟩
  * [.init(\_:value:format:prompt:)](https://developer.apple.com/documentation/swiftui/textfield/init\(_:value:format:prompt:\)-7flsn) ⭐️ create a text field that binds to a bound value, using a [`ParseableFormatStyle`](https://developer.apple.com/documentation/foundation/parseableformatstyle) to convert to and from this type.
    {% endtab %}

{% tab title="👥 相關" %}

{% endtab %}
{% endtabs %}

## History

{% tabs %}
{% tab title="✏️" %}

1. 2022.03.31
   {% endtab %}

{% tab title="1" %}

```
struct WeSplitApp: View {
    
    @State private var checkAmount = 0.0
    @State private var numberOfPeople = 2
    @State private var tipPercentage = 20
    
    let tipPercentages = [10, 15, 20, 25, 0]
    
    var body: some View {
        NavigationView {
            Form {
                Section {
                    // ⭐️ TextField with format
                    TextField(
                        "Amount", 
                        value: $checkAmount, 
                        format: .currency(code: Locale.current.currencyCode ?? "USD")
                    )
                    // ---------------------------------------------------
                    // ❗️ this won't stop users from entering other values
                    //   if they have a hardware keyboard or 
                    //   if they copy/paste text from elsewhere.
                    // ---------------------------------------------------
                    .keyboardType(.decimalPad)
                }
                
                Section {
                    Text(checkAmount, format: .currency(code: Locale.current.currencyCode ?? "TWD"))
                }
            }
            .navigationTitle("We Split")
            .navigationBarTitleDisplayMode(.inline)
        }
    }

```

{% endtab %}
{% endtabs %}
