iphone - UIKeyboardTypeDecimalPad - change comma to dot -
i use method show keyboard decimal separator
mytextfield.keyboardtype=uikeyboardtypedecimalpad;
how can change comma dot separator?
i have finnish locale. comma, decimals doesn't work on app.
-(ibaction)calculate { float x = ([paino.text floatvalue]) / ([pituus.text floatvalue]) *10000; label.text = [[nsstring alloc] initwithformat:@"%0.02f", x]; }
ok edit text field using numeric keyboard, dependent on current locale, , text representation of number, dependendt on current locale, too. after editing has finished read , want transform number.
to convert use nsnumberformatter this:
nsnumberformatter *nf = [[nsnumberformatter alloc] init];
you can setup formatter will, setting locale (!), limits, formatting, decimal/grouping separator, number of decimal digits etc. use it:
float number = [nf numberfromstring: field.text];
and that's all! have number if text includes comma, provided let both: keyboard , formatter, have same format, same style - i.e. let current locale used on place.
edit
this currency formatter, can convert between string , number currencies:
nsnumberformatter *nf = [[nsnumberformatter alloc] init]; [nf setnumberstyle: nsnumberformattercurrencystyle]; [nf setroundingmode: nsnumberformatterroundhalfup]; [nf setmaximumfractiondigits: 2]
this percentage formatter 4 decimal places:
nsnumberformatter *nf = [[nsnumberformatter alloc] init]; [nf setnumberstyle: nsnumberformatterpercentstyle]; [nf setroundingmode: nsnumberformatterroundhalfup]; [nf setmaximumfractiondigits: 4];
both in current locale.
as see can define style, digits, rounding behaviour , more, depending on numbers trying enter. more details (it lot can nsnumberformatter) should read apple docs, go beyond scope of answer describe all.
in case, provided paino , pituus uitextfields:
-(ibaction)calculate { nsnumberformatter *nf = [[nsnumberformatter alloc] init]; [nf setroundingmode: nsnumberformatterroundhalfup]; [nf setmaximumfractiondigits: 2]; float npaino = [[nf numberfromstring: paino.text] floatvalue]; float npituus = [[nf numberfromstring: pituus.text] floatvalue]; float x = npaino] / npituus *10000; label.text = [nf stringfromnumber: [nsnumber numberwithfloat: x]]; [nf release]; }
now avoid creating formatter in each calculation make instance variable, since need 1 conversions.
Comments
Post a Comment