UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: SwiftUI - How can calculations in Swift recognise "," (comma) and not only "." (decimal Point)?

Forums > SwiftUI

How can Calculations recognise "," (comma) in a inserted textfield?

Why im asking is because .keyboardType(.decimalPad) has a (decimal dot) "." but in Austria for example a "," (comma) is set automatically by the OS. If this is inserted in a Textfield it breaks the Calculation. With decimal dot "." everything works fine. Also don't exactly know how to use NSNumberFormatter. Found a simmilar topic about this but none of the solutions worked.

UserData:

import SwiftUI
import Combine

class UserData : ObservableObject {

    private static let userDefaultBuyingPrice = "BuyingPrice"
    private static let userDefaultRent = "Rent"
    private static let userDefaultlivArea = "livArea"

    @Published var BuyingPrice = UserDefaults.standard.string(forKey: UserData.userDefaultBuyingPrice) ?? "" {
    didSet {
      UserDefaults.standard.set(self.BuyingPrice, forKey: UserData.userDefaultBuyingPrice)
      }
    }

    @Published var Rent = UserDefaults.standard.string(forKey: UserData.userDefaultRent) ?? "" {
    didSet {
      UserDefaults.standard.set(self.Rent, forKey: UserData.userDefaultRent)
      }
    }

     @Published var livArea = UserDefaults.standard.string(forKey: UserData.userDefaultlivArea) ?? "" {
      didSet {
       UserDefaults.standard.set(self.livArea, forKey: UserData.userDefaultlivArea)
      }
    }

    private var canc: AnyCancellable!

}

ContentView:

import SwiftUI
struct ContentView: View {
    @EnvironmentObject var userData: UserData

    var sqmPrice: Double{
        let price = Double(userData.BuyingPrice) ?? 0
        let area = Double(userData.livArea) ?? 0

        let sqmPrice = price / area

        return sqmPrice
    }

    var sqmMonth: Double{
        let rent = Double(userData.Rent) ?? 0
        let area = Double(userData.livArea) ?? 0

        let sqmMonth = rent / area

        return sqmMonth
    }

    var body: some View {

            VStack {
                HStack{
                    Text ("Price €")
                    Spacer()
                    Text ("\(sqmPrice.isNaN ? 0 : sqmPrice, specifier: "%.1f") € /m²")
                }
                    TextField("100.000 €", text: $userData.BuyingPrice)
                    .font(.title)
                    .keyboardType(.decimalPad)
            }

                VStack {
                HStack{
                    Text ("Net Rent €")
                    Spacer()
                    Text ("\(sqmMonth.isNaN ? 0 : sqmMonth, specifier: "%.1f") € /m²")
                    }
                    TextField("500 €", text: $userData.Rent)
                    .font(.title)
                    .keyboardType(.decimalPad)
                }

                      VStack{
                HStack{
                    Text ("Area")
                    Spacer()
                    Text ("m²")
                }
                TextField("50", text: $userData.livArea)
                    .font(.title)
                    .keyboardType(.decimalPad)
               }
             }

Thanks for taking your time.

3      

i would if let [variableName] = Double([variableName].replacingOccurrences(of: ",", with: ".")) and then if it unwraps you can use that number for whatever you need

4      

you can also create an extension for Double which replaces the "," or keeps the "."

i used the extension from here https://stackoverflow.com/questions/28313871/swift-playground-how-to-convert-a-string-with-comma-to-a-string-with-decimal

4      

Thanks, both solutions work great.

3      

Thanks! Had the same problem, it was driving me crazy!

3      

Hacking with Swift is sponsored by RevenueCat

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

Learn more here

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.