1

How to set currency format E.g. 2,000,000.00 in swift on a textfield using swift 3.2 ? I am getting this response when in text field when i am entering amount in textfield

CharacterView(_core: Swift._StringCore(_baseAddress: Optional(0x00000001c464e38a), _countAndFlags: 13835058055282163718, _owner: Optional(₹ 75.33)), _coreOffset: 1)

My Code is:

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


    // Construct the text that will be in the field if this change is accepted

    switch string {
    case "0","1","2","3","4","5","6","7","8","9":
        currentString += string
        formatCurrency(currentString)
    default:
        if string.characters.count == 0 && currentString.characters.count != 0 {
            currentString = String(currentString.characters.dropLast())
            formatCurrency(currentString)
        }
    }
    return false    }

func formatCurrency(_ string: String) {
    print("format \(string)")
    print(txtfieldamount.text!)
    let formatter = NumberFormatter()
    formatter.numberStyle = .currency
    //formatter.locale = findLocaleByCurrencyCode("NGN")
    let numberFromField = (NSString(string: currentString).doubleValue)/100
    let temp = formatter.string(from: NSNumber(value: numberFromField))
    self.txtfieldamount.text! = String(describing: temp!.characters.dropFirst())
    print(txtfieldamount.text!)
}
Vega
  • 27,856
  • 27
  • 95
  • 103
Ruchi
  • 13
  • 1
  • 5

0 Answers0