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 :

8 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
Edison hope
AUTHOR
8 November 2021 at 01:27 delete

The information you provided is very useful, thank you very much for sharing useful information with us. You can apply for an online Turkey visa usa - If you are Planning a Trip to Turkey for business, tourism purpose.

Reply
avatar
26 December 2021 at 12:52 delete

This is a wonderful inspiring article. I am practically satisfied with your great work. You have really put together extremely helpful data. Keep it up... Continue... Getting a kenya e-visa is quite convenient. Anyone can apply at any time of the day.

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
Isabella Ava
AUTHOR
20 March 2022 at 01:15 delete

I think this is an informative post and it is very useful and informative. So, I would like to thank you for the effort you put into writing this article. The Indian e visa eta or Electronic Visa (eTV) is an online travel authorization. You can fill an Indian e visa application via the India e visa online website.

Reply
avatar