0

BMI = [weight(kg)]/ [height(m)] (*Formula given)

import UIKit

class BMIViewController: UIViewController {
    
    
    @IBOutlet weak var txtHeight: UITextField!
    
    @IBOutlet weak var txtWeight: UITextField!
    
    @IBOutlet weak var lblOutput: UILabel!
    
    @IBAction func btnCalculateBMI(_ sender: Any) {
        
        let  height = Double(txtHeight.text!)
        let weight = Double(txtWeight.text!)
        
        let bmi = weight! / (height! * height!)
         
        
        lblOutput.text = String(bmi)
    }
}

When running the application, there was too much decimals. How am I able to fix this issue?enter image description here

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • When presenting numbers to users, always use `NumberFormatter`. The same is valid when parsing numeric input from the user. – Sulthan Aug 25 '21 at 08:04
  • 1
    Force unwrapping the text to number conversion when dealing with user input is not a good approach. You are assuming the user is going to enter numbers only. You might have set up a numeric keypad to only allow numbers, but that doesn't help if the user uses copy-paste to insert whatever they like. – Abizern Aug 25 '21 at 08:46

0 Answers0