Swift:如何删除NSNumberFormatter的.00?

时间:2022-09-06 19:25:14

Someone helps me to make the textfield showing comma and $, but whenever there will always be .00 tailing the number even I delete all in the textfield. How to remove that if someone is only typing integer or nothing? Thanks a lot!

有人帮我制作显示逗号和$的文本字段,但只要总是有.00拖尾数字,即使我在文本字段中删除所有内容。如果有人只输入整数或什么都没有,如何删除?非常感谢!

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let oldText = textField.text! as NSString
    var newText = oldText.stringByReplacingCharactersInRange(range, withString: string) as NSString!
    var newTextString = String(newText)

    let digits = NSCharacterSet.decimalDigitCharacterSet()
    var digitText = ""
    for c in newTextString.unicodeScalars {
        if digits.longCharacterIsMember(c.value) {
            digitText.append(c)
        }
    }

    let formatter = NSNumberFormatter()
    //        formatter.usesSignificantDigits = false
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    formatter.locale = NSLocale(localeIdentifier: "en_US")
    let numberFromField = (NSString(string: digitText).doubleValue) / 100

    newText = formatter.stringFromNumber(numberFromField)

    textField.text = String(newText)

    return false

}

2 个解决方案

#1


0  

Try this,

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


    let oldText = textField.text! as NSString
    var newText = oldText.stringByReplacingCharactersInRange(range, withString: string) as NSString!
    var newTextString = String(newText)

    let digits = NSCharacterSet.decimalDigitCharacterSet()
    var digitText = ""
    for c in newTextString.unicodeScalars {
        if digits.longCharacterIsMember(c.value) {
            digitText.append(c)
        }
    }

    let formatter = NSNumberFormatter()
    //        formatter.usesSignificantDigits = false
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    formatter.locale = NSLocale(localeIdentifier: "en_US")
    let numberFromField = (NSString(string: digitText).doubleValue) / 100

    newText = formatter.stringFromNumber(numberFromField)

    if newText .isEqualToString("$0.00") {

        newText = ""

    }

    textField.text = String(newText)

    return false

}

I have just added,

我刚刚补充说,

  if newText .isEqualToString("$0.00") {

        newText = ""

    }

in your method.

在你的方法。

Update : (as asked in comment)

更新:(如评论中所述)

It's much simple replace your method with below one,

用下面的方法替换你的方法非常简单,

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


    let oldText = textField.text! as NSString
    var newText = oldText.stringByReplacingCharactersInRange(range, withString: string) as NSString!
    var newTextString = String(newText)

    let digits = NSCharacterSet.decimalDigitCharacterSet()
    var digitText = ""
    for c in newTextString.unicodeScalars {
        if digits.longCharacterIsMember(c.value) {
            digitText.append(c)
        }
    }

  //  let formatter = NSNumberFormatter()
    //        formatter.usesSignificantDigits = false
  //  formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
  //  formatter.locale = NSLocale(localeIdentifier: "en_US")

    let numberFromField = (NSString(string: digitText).intValue)



    newText = NSString.localizedStringWithFormat("$%d", numberFromField)

 //        if newText .isEqualToString("$0.00") {
//            
//            newText = ""
//        
//        }

    textField.text = String(newText)

    return false

}

#2


1  

set minimumFractionDigits and maximumFractionDigits of NSNumberFormatter()::

设置NSNumberFormatter()的minimumFractionDigits和maximumFractionDigits ::

formatter.minimumFractionDigits = 1
formatter.maximumFractionDigits = 1

#1


0  

Try this,

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


    let oldText = textField.text! as NSString
    var newText = oldText.stringByReplacingCharactersInRange(range, withString: string) as NSString!
    var newTextString = String(newText)

    let digits = NSCharacterSet.decimalDigitCharacterSet()
    var digitText = ""
    for c in newTextString.unicodeScalars {
        if digits.longCharacterIsMember(c.value) {
            digitText.append(c)
        }
    }

    let formatter = NSNumberFormatter()
    //        formatter.usesSignificantDigits = false
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    formatter.locale = NSLocale(localeIdentifier: "en_US")
    let numberFromField = (NSString(string: digitText).doubleValue) / 100

    newText = formatter.stringFromNumber(numberFromField)

    if newText .isEqualToString("$0.00") {

        newText = ""

    }

    textField.text = String(newText)

    return false

}

I have just added,

我刚刚补充说,

  if newText .isEqualToString("$0.00") {

        newText = ""

    }

in your method.

在你的方法。

Update : (as asked in comment)

更新:(如评论中所述)

It's much simple replace your method with below one,

用下面的方法替换你的方法非常简单,

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


    let oldText = textField.text! as NSString
    var newText = oldText.stringByReplacingCharactersInRange(range, withString: string) as NSString!
    var newTextString = String(newText)

    let digits = NSCharacterSet.decimalDigitCharacterSet()
    var digitText = ""
    for c in newTextString.unicodeScalars {
        if digits.longCharacterIsMember(c.value) {
            digitText.append(c)
        }
    }

  //  let formatter = NSNumberFormatter()
    //        formatter.usesSignificantDigits = false
  //  formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
  //  formatter.locale = NSLocale(localeIdentifier: "en_US")

    let numberFromField = (NSString(string: digitText).intValue)



    newText = NSString.localizedStringWithFormat("$%d", numberFromField)

 //        if newText .isEqualToString("$0.00") {
//            
//            newText = ""
//        
//        }

    textField.text = String(newText)

    return false

}

#2


1  

set minimumFractionDigits and maximumFractionDigits of NSNumberFormatter()::

设置NSNumberFormatter()的minimumFractionDigits和maximumFractionDigits ::

formatter.minimumFractionDigits = 1
formatter.maximumFractionDigits = 1