objective c - Get A Number From NSString that may have grouping separator, (comma, decimal, space) -


i have several text fields user enters number , various calculations number. since these numbers can quite large, have feeling users might put grouping separators (commas, spaces, decimals) in entered value.

is there way check if user has put grouping separator in textfield , delete grouping separator can store entered value nsnumber?

from can tell, nsnumberformatter not provide method , of solutions seem inefficient. wondering if guys had ways deal this.

nsnumberformatter provide ways deal commas, dots , spaces, in fact, well. there's "leniency" tries guess best formatting:

nsnumberformatter *formatter = [[nsnumberformatter alloc] init];  [formatter setnumberstyle:nsnumberformatterdecimalstyle]; [formatter setlenient:yes];  nslog(@"%@", [formatter numberfromstring:@"123,456.78"]); nslog(@"%@", [formatter numberfromstring:@"123 456 78"]); 

output:

123456.78 12345678 

it works, if user starts mixing things (e.g 123,456 789.105) fails. replace occurrences of spaces, , use number formatter.

if you're worried efficiency, need inform you: don't. it's text input used once every run. if doing every time user scrolls something, or thousands of calculations, should worry.

also, if user going input large numbers, don't think there better interfaces handling that? i'm not sure if you're writing ios or osx app, depends. maybe give him scientific notation set of buttons, can pick number , power?


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -