2017-08-26 68 views
0

這裏是強制性的「我是編程新手」,但是,我搜索了所有可用的答案,並得出結論認爲我的問題可能是比代碼更多的邏輯關係,但我可以對此也是錯誤的。我正在構建一個計算器應用程序,除了顯示屏中的numberFormatter(用於顯示逗號分隔符)之外,所有內容都正常工作。每當我嘗試在顯示器中設置數字格式時,我都無法使顯示屏顯示小數點和逗號。數字格式化程序不允許小數顯示

如果我以小數點開始,.1234,我得到0.1234,如果我輸入12345,我得到12,345,但如果我輸入12345.678,我得到12,345。我失去了小數。我已經測試了它,並且我的功能可以刪除多餘的「。」。似乎不是問題。如果我在標籤格式控制外運行字符串擴展numberFormatter它似乎工作,但我需要防止多個小數和多餘的「0」。

我正在顯示IBAction的代碼,其中包含顯示標籤上顯示的按鈕display.text這是問題。在此之後的所有計算工作正常,replacingOccurrences(of: ",", with: "")創建一個乾淨的字符串轉換爲Double並計算。

我正在使用sting擴展來進行格式化。我一直在爲此工作數星期。有任何想法嗎?我是否必須重構如何在label.text中輸入文字?

這裏是將文本添加到UILabel display的代碼。

@IBAction func btnTouchDigit(_ sender: UIButton) { 

     let digit = sender.currentTitle! 

     if isUserTyping { 
      var formattedNumber = "" 

      print("is user typting + String\(isUserTyping)") 

      // make sure we aren't adding a second period 

      var textCurrentlyInDisplay = display.text 
      textCurrentlyInDisplay = textCurrentlyInDisplay?.replacingOccurrences(of: ",", with: "") 

      if digit == "." && ((textCurrentlyInDisplay?.range(of: ".")) != nil) { 
       return 
      } 
      else { 
       formattedNumber = (textCurrentlyInDisplay! + digit) 
       print("formattedNumber = \(formattedNumber.twoFractionDigits)") 
       display.text = formattedNumber.twoFractionDigits 

       // put code here to format label.text to show thousand seperators 
       print("textCurrentlyInDisplay end = \(textCurrentlyInDisplay!)")  
      } 
     } 

      // make sure we aren't entering a bunch of zero's   
     else { print("else + \(isUserTyping)") 

      display.text = digit 
      if digit == "0" {return} 
      else if digit == "." {display.text = "0."} 
      // display.text = (digit == "." ? "0" : "") + digit 
      isUserTyping = true 
     } 

    } 

這是我的擴展,用於處理numberFormatter的字符串轉換。

extension String { 
    var twoFractionDigits: String { 
     let styler = NumberFormatter() 
     styler.minimumFractionDigits = 0 
     styler.maximumFractionDigits = 16 
     styler.numberStyle = .decimal 
     let converter = NumberFormatter() 
     converter.decimalSeparator = "." 
     if let result = converter.number(from: self) { 
      return styler.string(from: result)! 
     } 
     return "" 

    } 
+0

有你'twoFractionDigits'方法沒有問題。按照預期,在「1234.56」結果中輸入「1,234.56」。所以你的問題在別處。使用你的調試器,看看你輸入'1234.'後會發生什麼,然後點擊'5'。 – rmaddy

+0

我在btnTouchDigit函數的最後放置了一個斷點,並用12345.111逐步完成,仍然得到12,345,111。沒有錯誤,也沒有小數。 –

回答

0

我發現了一個黑客來解決我的問題。這並不漂亮,但它的工作原理。我能夠讓numberFormatter更新並顯示小數點後的數字,但這導致了一個新問題。如果你輸入12345.00,你會得到12,344,直到你按下另一個數字時纔看到尾部0。 ex 12345.1→12,345.1,12345.001→12,345.001,但是12345.00→> 12,345。

我需要它動態工作,以便用戶知道輸入了多少個零。我的破解是把最後的數量分成兩個數組。一個預格式化和一個後格式化。然後將兩者一起加入最終顯示編號。

如果有人有任何想法,我仍然樂意找到更有效的解決方案。

這裏是更新的代碼。

@IBAction func btnTouchDigit(_ sender:UIButton){ let digit = sender.currentTitle!

if isUserTyping { 
     preFormattedNumber = (display.text?.replacingOccurrences(of: ",", with: ""))! 

     // set the limit for number of digits user can enter at once 
     if display.text!.count >= 16 { 
      return 
     } 

     else { 
      // make sure we aren't adding a second period 
      if digit == "." && ((preFormattedNumber.range(of: ".")) != nil) { 

       print("extra decimal pressed") 
       return 
      } 
      else { 
       preFormattedNumber = (preFormattedNumber + digit) 
       print("preFormattedNumber before Formatting = \(preFormattedNumber)") 
      } 

      // put code here to format label.text to show thousand seperators 
      if ((preFormattedNumber.range(of: ".")) != nil){ 
       print("just checked for .") 
       let numPreFormat = preFormattedNumber 
       let numAfterFormat = preFormattedNumber.twoFractionDigits 
       let numArray = numPreFormat.components(separatedBy: ".") 
       let numArrayFormatted = numAfterFormat.components(separatedBy: ".") 
       let afterDecimal = numArray.last 
       let beforeDecimal = numArrayFormatted.first 
       let finalNumberToDisplay = beforeDecimal! + "." + afterDecimal! 
       print("numArray = \(numArray)") 
       print("final number to display = \(finalNumberToDisplay)") 
       print("numArray = \(numArray)") 
       display.text = finalNumberToDisplay 
       runningNumber = display.text! 
      } 
      else { 
       display.text = preFormattedNumber.twoFractionDigits 
       runningNumber = display.text! 
      } 
     } 
    } 

     // make sure we aren't entering a bunch of zero's 
    else { print("else + \(isUserTyping)") 
     preFormattedNumber = digit 
     display.text = preFormattedNumber 
     runningNumber = display.text! 
     if digit == "0" {return} 
     else if digit == "." { preFormattedNumber = "0."; 
     } 

     display.text = preFormattedNumber 
     runningNumber = display.text! 
     isUserTyping = true 
    } 


} 
相關問題