title: Converting String to Currency Swift
body: <p>I am having trouble converting my array of String to Currency.</p>
  
  <p><a href="https://i.stack.imgur.com/fxUzt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fxUzt.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/GBRcK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GBRcK.png" alt="enter image description here"></a></p>
  
  <p>I have created an extension <code>currencyInputFormatting()</code>. However, the commas are being placed in the wrong spots. </p>
  
  <p>Here is my code :-</p>
  
  <pre><code>cell.balanceLabel.text? = (monthlyBalanceStringArray)[indexPath.row].currencyFormatting()
  
  extension String {
  
      // formatting text for currency textField
      func currencyFormatting() -&gt; String {
  
          var number: NSNumber!
          let formatter = NumberFormatter()
          formatter.numberStyle = .currency
          formatter.maximumFractionDigits = 2
  
          var amountWithPrefix = self
  
          let regex = try! NSRegularExpression(pattern: "[^0-9]", options: .caseInsensitive)
          amountWithPrefix = regex.stringByReplacingMatches(in: amountWithPrefix, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.characters.count), withTemplate: "")
  
          let double = (amountWithPrefix as NSString).doubleValue
          number = NSNumber(value: (double))
  
          //    number = NSNumber(value: (double / 100))
  
          guard number != 0 as NSNumber else {
              return ""
          }
  
          return formatter.string(from: number)!
      }
  }
  </code></pre>