## @metropol: Day 18 - WeSplit challenge

 Feb '23 Thought of posting my solution to the challenge in the WeSplit: Wrap up, including the currency formatter closure. I've left a couple of lines commented out -- those were to have the segmented picker for the tip percentage. ``````struct ContentView: View { @State private var checkAmount = 0.0 @State private var numberOfPeople = 0 @State private var tipPercentage = 20 @FocusState private var amountIsFocused: Bool // private let tipPercentages = [10, 15, 20, 25, 0] private let tipPercentages = 0...100 private var totalPerPerson: Double { let peopleCount = Double(numberOfPeople + 2) let amountPerPerson = grandTotal / peopleCount return amountPerPerson } private var grandTotal: Double { let tipPercentage = Double(tipPercentage) let tipValue = tipPercentage * checkAmount / 100 let grandTotal = checkAmount + tipValue return grandTotal } private var currency: FloatingPointFormatStyle.Currency { .currency(code: Locale.current.currency?.identifier ?? "USD") } var body: some View { NavigationView { Form { Section { TextField("Amount", value: \$checkAmount, format: currency) .keyboardType(.decimalPad) .focused(\$amountIsFocused) } Picker("Number of people", selection: \$numberOfPeople) { ForEach(2 ..< 100) { Text("\(\$0) people") } } Section { Picker("Tip percentage", selection: \$tipPercentage) { ForEach(tipPercentages, id: \.self) { Text(\$0, format: .percent) } } // .pickerStyle(.segmented) } header: { Text("How much tip do you want to leave?") } Section { Text(grandTotal, format: currency) } header: { Text("Grand Total") } Section { Text(totalPerPerson, format: .currency(code: Locale.current.currency?.identifier ?? "USD")) } header: { Text("Amount per person") } } .navigationTitle("WeSplit") .toolbar { ToolbarItemGroup(placement: .keyboard) { Spacer() Button("Done") { amountIsFocused = false } } } } } }`````` 2

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Sponsor Hacking with Swift and reach the world's largest Swift community!

### Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

You are not logged in