I'm looking for a way to format a string into currency using the UITextField in language swift for iOS 8
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 NSArrayvar 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) / 100var 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").invertedSetvar 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+
5 comments
Write commentsAttractive 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
ReplySomeone 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
ReplyThe 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
ReplyNice 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.
ReplyI 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