【问题标题】:Dynamically format a number to have commas in a UITextField动态格式化数字以在 UITextField 中使用逗号
【发布时间】:2018-07-18 16:36:51
【问题描述】:

如何将价格文本字段文本格式化为像 234,345,567 这样的货币格式,并且我需要将小数点限制为不超过 2,并在用户开始输入时附加 $ 符号。

例如:$234,345,678.25

像这样,当他们在文本字段中输入金额时,我想在 3 个数字之间添加逗号

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {


if ((string == "0" || string == "") && (txtFldPostalCode.text! as NSString).range(of: ".").location < range.location) {
  return true
}
let cs = NSCharacterSet(charactersIn: "0123456789.").inverted
let filtered = string.components(separatedBy: cs)
let component = filtered.joined(separator: "")
let isNumeric = string == component
if isNumeric {
  let formatter = NumberFormatter()
  formatter.numberStyle = .decimal
  formatter.maximumFractionDigits = 8
  let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
  let numberWithOutCommas = newString.replacingOccurrences(of: ",", with: "")
  let number = formatter.number(from: numberWithOutCommas)
  if number != nil {
    var formattedString = formatter.string(from: number!)
    if string == "." && range.location == textField.text?.length {
      formattedString = formattedString?.appending(".")
    }
    textField.text = formattedString
  } else {
    textField.text = nil
  }
}
return false

}

【问题讨论】:

标签: ios swift3 uitextfield


【解决方案1】:

对于 Swift 3. 在文本字段中输入货币格式(从右到左)

@IBOutlet weak var textfield: UITextField!


override func viewDidLoad() {
    super.viewDidLoad()

    textfield.addTarget(self, action: #selector(myTextFieldDidChange), for: .editingChanged)
    // Do any additional setup after loading the view.
}
@objc func myTextFieldDidChange(_ textField: UITextField) {

    if let amountString = textField.text?.currencyInputFormatting() {
        textField.text = amountString
    }
}
}
extension String {

// formatting text for currency textField
func currencyInputFormatting() -> String {

    var number: NSNumber!
    let formatter = NumberFormatter()
    formatter.numberStyle = .currencyAccounting
    formatter.currencySymbol = "$"
    formatter.maximumFractionDigits = 2
    formatter.minimumFractionDigits = 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 / 100))


    guard number != 0 as NSNumber else {
        return ""
    }

    return formatter.string(from: number)!
}

希望对您有所帮助...

【讨论】:

  • 我想要这个从左到右,但我不想要那个小数,我已经试过了。如果我输入小数,我必须用 2 位数字来限制它
  • 你想要什么,我没有得到你??正如你所问的,我希望小数点不超过 2 ...所以在我的回答中,它仅限于 2 位
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 2020-12-18
  • 2017-12-09
  • 2011-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多