title: Limit the Calculated Value to a max Value - SwiftUI
body: <p>If Values are inserted in the app and not in the "lets say prefer order" a big value occurs, it is correct but it doesn't look good. </p>
  
  <p>For example if the second Textfield (userData.Rent) is filled before the first Textfield(userData.BPrice) a value for the "return Brutto" is 24 thousand % ... </p>
  
  <p>How can the calculated value be limited to a certain value, for example it cannot go higher than 20% or any other value. But below this value it moves freely. </p>
  
  <p>It should more or less stop at lets say 20%</p>
  
  <p>// ContentView: </p>
  
  <pre><code>struct ContentView: View {
      @EnvironmentObject var userData: UserData
  
      var Brutto: Double{
          let price = Double(userData.BPrice) ?? 0
          let rent = Double(userData.Rent) ?? 0
  
          let Brutto = rent * 12 / price * 100
  
          return Brutto
      }
  
      var body: some View {
          VStack {            
              Text ("\(Brutto, specifier: "%.1f")")
          }
          VStack{
               TextField("BPrice", text: $userData.BPrice)
                 .keyboardType(.decimalPad)
           }
           VStack{
                TextField("Rent", text: $userData.Rent)
                 .keyboardType(.decimalPad)
            }
      }
  </code></pre>
  
  <p>// UserData File: </p>
  
  <pre><code>class UserData : ObservableObject {
  
    private static let userDefaultBPrice = "BPrice"
    private static let userDefaultRent = "Rent"
  
  
    @Published var BuyingPrice = UserDefaults.standard.string(forKey: UserData.userDefaultBPrice) ?? "" 
    {
      didSet {
        UserDefaults.standard.set(self.BPrice, forKey: UserData.userDefaultBPrice)
      }
    }
  
    @Published var Rent = UserDefaults.standard.string(forKey: UserData.userDefaultRent) ?? "" {
      didSet {
        UserDefaults.standard.set(self.Rent, forKey: UserData.userDefaultRent)
      }
    }
    private var canc: AnyCancellable!
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/KuEdm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KuEdm.jpg" alt="Screenshot were the absurd high value is displayed"></a></p>
  
  <p>Thanks</p>