Swift数字格式化程序货币问题

Swift数字格式化程序货币问题,swift,Swift,我希望我的文本字段显示5.000,00例如,我设法限制了用户可以键入的字符,但我的货币不起作用。另外,我对Swift还不熟悉,我该如何让这种货币发挥作用 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let formatter = NumberFormatter() f

我希望我的文本字段显示
5.000,00
例如,我设法限制了用户可以键入的字符,但我的货币不起作用。另外,我对Swift还不熟悉,我该如何让这种货币发挥作用

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

    let formatter = NumberFormatter()
    formatter.numberStyle = .currency
    formatter.allowsFloats = true
    formatter.currencyDecimalSeparator = ","
    formatter.alwaysShowsDecimalSeparator = true
    formatter.locale = Locale(identifier: "pt_BR")
    formatter.currencyCode = "BRL"
    formatter.maximumFractionDigits = 0

    let currentText = textField.text ?? ","
    guard let stringRange = Range(range, in: currentText) else { return false}
    let updatedText = currentText.replacingCharacters(in: stringRange, with: string)

    if let groupingSeparator = formatter.groupingSeparator {

        if string == groupingSeparator {
            return true
        }


        if let textWithoutGroupingSeparator = textField.text?.replacingOccurrences(of: groupingSeparator, with: "") {
            var totalTextWithoutGroupingSeparators = textWithoutGroupingSeparator + string
            if string.isEmpty { // pressed Backspace key
                totalTextWithoutGroupingSeparators.removeLast()
            }
            if let numberWithoutGroupingSeparator = formatter.number(from: totalTextWithoutGroupingSeparators),
                let formattedText = formatter.string(from: numberWithoutGroupingSeparator) {

                textField.text = formattedText
                return false
            }
            return updatedText.count <= 8
        }
    }
    return true
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
let formatter=NumberFormatter()
formatter.numberStyle=.currency
formatter.allowsFloats=true
formatter.currencyDecimalSeparator=“,”
formatter.AlwaysShowsCIMalSeparator=true
formatter.locale=locale(标识符:“pt\u BR”)
formatter.currencyCode=“BRL”
formatter.maximumFractionDigits=0
让currentText=textField.text??“,”
guard let stringRange=Range(范围,in:currentText)else{return false}
让UpdateText=currentText.replacingCharacters(在:stringRange中,带:string)
如果让groupingSeparator=formatter.groupingSeparator{
如果字符串==groupingSeparator{
返回真值
}
如果let textWithoutGroupingSeparator=textField.text?.replacingOccurrences(of:groupingSeparator,with:){
var totalTextWithoutGroupingSeparators=textWithoutGroupingSeparator+string
如果string.isEmpty{//按了退格键
totalTextWithoutGroupingSeparators.removeLast()的总文本
}
如果let numberWithoutGroupingSeparator=formatter.number(from:totaltext withoutgroupingseparator),
让formattedText=formatter.string(from:numberwhithoutGroupingSeparator){
textField.text=格式化文本
返回错误
}

return updateText.count我用一个字符串扩展名来解决这个问题,以前我在视图控制器上使用一个函数,用这种方式为我解决问题,你只需要更改你的区域设置就可以了

扩展字符串{

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

    var number: NSNumber!
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.locale = Locale(identifier: "pt_BR")
    formatter.currencySymbol = ""
    formatter.maximumFractionDigits = 2
    formatter.minimumFractionDigits = 2

    var amountWithPrefix = self

    // remove from String: "$", ".", ","
    let regex = try! NSRegularExpression(pattern: "[^0-9]", options: .caseInsensitive)
    amountWithPrefix = regex.stringByReplacingMatches(in: amountWithPrefix, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, self.count), withTemplate: "")

    let double = (amountWithPrefix as NSString).doubleValue
    number = NSNumber(value: (double / 100))

    // if first number is 0 or all numbers were deleted
    guard number != 0 as NSNumber else {
        return ""
    }
    print(formatter.string(from: number))
    return formatter.string(from: number)!
}

}

“但是我的货币不起作用”这是什么意思?我的意思是我希望这个数字500000在文本字段中以这种方式显示,现在是50000000,这意味着它的格式不正确。您可以使用自定义货币字段,只需将区域设置固定为“pt\u BR”
谢谢你们,问题解决了,我刚将货币设置为“pt\u BR”。