1 Reply
      Latest reply on Jul 12, 2019 10:42 AM by Claude31
      Arch-Dict Level 1 Level 1 (0 points)
        import UIKit
        
        class ViewController: UIViewController {
        
            @IBOutlet weak var resultLabel: UILabel!
        
            var userIsInTheMiddleOfTyping : Bool = false
            
            var usingDecimal = false
            
            var firstNumber : Double = 0.0
            var secondNumber : Double = 0.0
            var result : Float = 0.0
            var choosingOperator : String = ""
            let π = 3.14159265359
            
           
            
            override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view.
            }
        
        
            @IBAction func clearButtonTapped(_ sender: UIButton) {
            
                //resultLabel.text = "C"
                
                firstNumber = 0.0
                secondNumber = 0.0
                result = 0.0
                choosingOperator = ""
                resultLabel.text = ""
                usingDecimal = false
            }
            
            
            
            @IBAction func operatorButtonTapped(_ sender: UIButton) {
                
                // +,-,/,*
               
                if resultLabel.text != "." {
                    
                    
                    firstNumber = Double(resultLabel.text!)!
                    
                    print(firstNumber)
                    
                    userIsInTheMiddleOfTyping = false
            
                    choosingOperator = sender.currentTitle!
                    usingDecimal = false
                }
                
            }
            
            // other operators 
            
            @IBAction func cubicOrQuadraticButtonTapped(_ sender: UIButton) {
                
                firstNumber = Double(resultLabel.text!)!
                choosingOperator = sender.currentTitle!
                
                switch choosingOperator {
                
                    case "x²": result = Float(firstNumber * firstNumber)
                    case "√": result = Float(sqrt(firstNumber))
                    case "x³": result = Float(firstNumber * firstNumber * firstNumber)
                    case "∛": result = Float(cbrt(firstNumber))
                    
                default: break
                }
                
                resultLabel.text = String(result)
                userIsInTheMiddleOfTyping = false
                usingDecimal = false
                
                
            }
            
            @IBAction func trigButtonTapped(_ sender: UIButton) {
                
                firstNumber = Double(resultLabel.text!)!
                choosingOperator = sender.currentTitle!
                
                switch choosingOperator {
                    
                case "sin⁻¹": result = Float(asin(firstNumber) * 180/π)
                case "cos⁻¹": result = Float(acos(firstNumber) * 180/π)
                case "tan⁻¹": result = Float(atan(firstNumber) * 180/π)
                    
                default: break
                }
                
                resultLabel.text = String(result)
                userIsInTheMiddleOfTyping = false
                usingDecimal = false
                
            }
            
            
            
            // Number Tapped
            
            @IBAction func keypadButtonTapped(_ sender: UIButton) {
                // resultLabel.text = "Keypad tapped"
                var key = sender.currentTitle!
                print(key)
                if userIsInTheMiddleOfTyping == true {
                    if usingDecimal == true && key == "."{
                        key = ""
                    }
                    resultLabel.text = resultLabel.text! + key
                } else {
                    resultLabel.text = key
                    userIsInTheMiddleOfTyping = true
                }
                if key == "." {
                    usingDecimal = true
                }
            }
            // Equals tapped
            @IBAction func equalsButtonTapped(_ sender: UIButton) {
               // resultLabel.text = "Equals Tapped"
                
                secondNumber = Double(resultLabel.text!)!
                
                calculate(_operator: choosingOperator)
                
                resultLabel.text = String(result)
        
                userIsInTheMiddleOfTyping = false
                usingDecimal = false
             
            }
            
            
            func calculate(_operator: String) {
                
                switch _operator {
                    case "": result = Float(firstNumber + secondNumber)
                    case "": result = Float(firstNumber - secondNumber)
                    case "": result = Float(firstNumber * secondNumber)
                    case "": result = Float(firstNumber / secondNumber)
                   
                    default: break
                }
            }
            
            
        }
        
        

         

        Hello Apple community!

        I am programming a calculator app and I would like to use the NumberFormatter to change the format of larger numbers in my results label.

         

        For example:

        345906483

        would than be

        345'906'483

         

        I have attempted to translate the number  using the Number Formatter, which worked

         

        
        let numberFormatter = NumberFormatter()
        numberFormatter.numberStyle = .decimal
        numberFormatter.groupingSeparator = "'"
        let formattedNumber = numberFormatter.string(from: NSNumber(value: result))
        print(formattedNumber!)
        
        resultLabel.text = formattedNumber
        userIsInTheMiddleOfTyping = false
        usingDecimal = false

         

        However, when I attempt to do further calculations, I have an issue:

         

        The number that stands in my label is now a string with numbers and " ' ", my new calculation doesn't work

         

        I then reversed this formatting at the beginning of each calculation, and then again at the end of calculation:

         

        let numberFormatterReverse = NumberFormatter()
        numberFormatterReverse.numberStyle = .none
        numberFormatterReverse.usesGroupingSeparator = false
        let formattedNumberReverse = numberFormatterReverse.string(from: NSNumber(value: result))
        print(formattedNumberReverse!)
        
        resultLabel.text = formattedNumberReverse
        firstNumber = Double(resultLabel.text!)!
        
        result = Float(sqrt(firstNumber))
        
        let numberFormatter = NumberFormatter()
        numberFormatter.numberStyle = .decimal
        numberFormatter.groupingSeparator = "'"
        let formattedNumber = numberFormatter.string(from: NSNumber(value: result))
        print(formattedNumber!)
        
        resultLabel.text = formattedNumber
        userIsInTheMiddleOfTyping = false
        usingDecimal = false
        

         

        I am sure that this is too complicated and I would appreciate some assistance!

        Thanks in advance :-)

        • Re: Format Number in a Label
          Claude31 Level 8 Level 8 (6,115 points)

          The error here is to consider the label as a storage. It is just for display.

           

          So, you need to keep the "raw" value (number) alongside what you display in label.

           

          Then, instead of calling

              @IBAction func trigButtonTapped(_ sender: UIButton) {
                  firstNumber = Double(resultLabel.text!)!

          You would just call

           

              @IBAction func trigButtonTapped(_ sender: UIButton) { 
                  firstNumber = result