textfield - iOS: NSNumberFormatter doesn't convert string back to NSNumber -
i using following nsnumberformatter add commas , symbol in currency value.
self.currencyformatter = [nsnumberformatter new]; self.currencyformatter.numberstyle = nsnumberformattercurrencystyle; self.currencyformatter.currencysymbol = @"£"; self.currencyformatter.currencycode = @"gbp"; self.currencyformatter.roundingmode = nsnumberformatterroundhalfup; self.currencyformatter.maximumfractiondigits = 0;
usage:
self.principleamounttextfield.text = [self.currencyformatter stringfromnumber:[nsnumber numberwithinteger:100000]];
this displays £100,000 expected. if insert 2 more digits (text becomes £100,00096) in textfield , try convert string integer 0! following line returns 0. have no idea how deal issue.
nslog(@"%d", [[self.currencyformatter numberfromstring:@"£100,00096"] integervalue]);
fyi have custom inputview textfield allows numbers enter textfield. in did edit end format number , display comma.
you need remove commas work, can still show them in text field should strip them out before pass string formatter. this:
nsstring *userinput = @"£100,00096"; userinput = [userinput stringbyreplacingoccurrencesofstring:@"," withstring:@""]; nslog(@"%ld", (long)[[currencyformatter numberfromstring:userinput] integervalue]);
Comments
Post a Comment