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()!
            }else
            {
                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 :

5 comments

Write comments
Unknown
AUTHOR
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

Reply
avatar
Unknown
AUTHOR
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

Reply
avatar
Melon
AUTHOR
6 December 2020 at 06:17 delete

The calculations have also been rigorously tested both mathematically and physically against virtually all available mortgage calculators we could find. mortgage calculator canada When Vancouver and Toronto real estate property prices dramatically inflated other parts of the country remained quite affordable. canada mortgage calculator

Reply
avatar
Robert smith
AUTHOR
9 February 2022 at 16:58 delete

Nice post. keep up the good work....India entry visa, You can get an online India entry visa via Indiaevisas website. Then the process of getting an Indian Tourist Visa will be quite simple and quick.

Reply
avatar
Allen smith
AUTHOR
11 February 2022 at 12:14 delete

I have read this article many times, and I found it very helpful. I really appreciate the time and effort you put into this and click here to apply for an urgent e visa to India. An e-visa to India has now become easier to obtain.

Reply
avatar