Wednesday, 28 January 2015

Jogendra Singh

Currency format in output in swift language

I'm looking for a way to format a string into currency using the UITextField  in language swift for iOS 8
Using NSNumberFormatter to format currency in output swift language

For example, i'd like to have the number "123456" converted into "$1,234.45" Or if I have a number under 1$, I would like it to look like this: "12" -> "$0.12"

I use this code. This work for me
1)  Add UITextField Delegate to header file
2) Add this code  

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

        if textField.tag == 100 && phoneNumberValidation(string) || string == "" && phoneNumberValidation(string)
            var cleanArray = textField.text.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet) as NSArray
            var cleanCentString  = cleanArray.componentsJoinedByString("")
            var centAmount:NSInteger! =  cleanCentString.toInt()
            if countElements(string) > 0
                if centAmount == nil
                    centAmount = 0
                centAmount = centAmount * 10 + string.toInt()!
                centAmount = centAmount / 10
            var floatValue = Double(centAmount) / 100
            var amount = NSNumber(double:  floatValue )
            var _currencyFormatter = NSNumberFormatter()
            _currencyFormatter.numberStyle = .CurrencyStyle
            _currencyFormatter.currencyCode = "USD"
            _currencyFormatter.negativeFormat = "-¤#,##0.00"
            textField.text = _currencyFormatter.stringFromNumber(amount)
        if textField.tag != 100 {
            return true
        return false
    func phoneNumberValidation(value: String) -> Bool {
        var charcter  = NSCharacterSet(charactersInString: "0123456789").invertedSet
        var filtered:NSString!
        var inputString:NSArray = value.componentsSeparatedByCharactersInSet(charcter)
        filtered = inputString.componentsJoinedByString("")
        return  value == filtered

Thanks for Visit  , Also can visit on My blog Facebook page iPhone & iPad Application Development Help World and also can visit Google+ 

Jogendra Singh

About Jogendra Singh -

I'm Founder of Jogendra.Com. I love to share my bright ideas with the whole blogging community. I'm a good iPhone Application Developer. Apart from Blogging, I love to play Cricket and mobile games (Clash of clans).

Subscribe to this Blog via Email :


Write comments
6 October 2017 at 08:24 delete

Attractive section of content. I just stumbled upon your site and in accession capital to assert that I acquire actually enjoyed account your blog posts. Anyway I will be subscribing to your augment and even I achievement you access consistently quickly. gmail sign in

Melony Fisher
5 May 2018 at 23:41 delete

Someone essentially help to make seriously articles I would state. This is the very first time I frequented your website page and up to now? I surprised with the analysis you made to make this particular post amazing. Wonderful activity! gmail login email