ios - Validate if a String is in currency format -


i trying create method in swift takes in string , returns bool. want return true if string in correct currency format , false if not.

so far method have tried:

func textisvalidcurrencyformat(text: string) -> bool {     var isvalidcurrencyformat = false      var numberformatter = nsnumberformatter()     numberformatter.numberstyle = nsnumberformatterstyle.currencystyle     var number = numberformatter.numberfromstring(text)      if number != nil {         isvalidcurrencyformat = true     }      return isvalidcurrencyformat } 

problem: works except want invalidate strings spaces before or after amount , if there more 2 numbers after decimal method accepts.

currently not care if method returns true or false based on currency type (a.k.a. usd or gbp or other currency) may want use reject currencies other specific type in future well.

edit:

this final code have come correctly determines if string entered in correct currency format , accounts edge cases. please, add suggestions in comments below!

func textisvalidcurrencyformat(text: string) -> bool {     var isvalidcurrencyformat = false      var numberformatter = nsnumberformatter()     numberformatter.numberstyle = nsnumberformatterstyle.currencystyle     var number = numberformatter.numberfromstring(text)      if number != nil {         let numberparts = text.componentsseparatedbystring(".")         if numberparts.count == 2 {             let decimalarray = array(numberparts[1])             if decimalarray.count <= 2 {                 if text == text.stringbytrimmingcharactersinset(nscharacterset.whitespaceandnewlinecharacterset()) {                     isvalidcurrencyformat = true                 }             }         } else {             if text == text.stringbytrimmingcharactersinset(nscharacterset.whitespaceandnewlinecharacterset()) {                 isvalidcurrencyformat = true             }         }      }      return isvalidcurrencyformat } 

how's this? return false if precision greater cents:

if double(number!) * 100 != floor(double(number!) * 100) {     isvalidcurrencyformat = false } 

and return false if there's whitespace before or after:

if text != text.stringbytrimmingcharactersinset(nscharacterset.whitespaceandnewlinecharacterset()) {     isvalidcurrencyformat = false } 

so putting together:

func textisvalidcurrencyformat(text: string) -> bool {     var isvalidcurrencyformat = false      var numberformatter = nsnumberformatter()     numberformatter.numberstyle = nsnumberformatterstyle.currencystyle     var number = numberformatter.numberfromstring(text)      if number != nil {         isvalidcurrencyformat = true         if double(number!) * 100 != floor(double(number!) * 100) {             isvalidcurrencyformat = false # or return false here         }     }      if text != text.stringbytrimmingcharactersinset(nscharacterset.whitespaceandnewlinecharacterset()) {         isvalidcurrencyformat = false # or return false here     }      return isvalidcurrencyformat } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -