ios - Converting number to format with NSNumberFormatter -
how can convert number 4273095782 number 4 273 095 782 nsnumberformatter?
i use diode not it
nsnumberformatter *formatter = [[nsnumberformatter alloc] init]; formatter.numberstyle = kcfnumberformatterdecimalstyle; formatter.maximumintegerdigits = 3; formatter.minimumfractiondigits = 3; formatter.groupingseparator = @" "; formatter.decimalseparator = @" "; nsstring *formattednumber = [formatter stringfromnumber:p];
you need use 2 properties : setgroupingsize:
last 3 digits , setsecondarygroupingsize:
other digits:
the complete implementation go this:
nsinteger integer = 4273095782; nsnumberformatter * formatter = [nsnumberformatter new]; [formatter setnumberstyle:nsnumberformatterdecimalstyle]; [formatter setgroupingsize:3]; [formatter setsecondarygroupingsize:3]; [formatter setgroupingseparator:@" "]; nsstring * threeseparated = [formatter stringfromnumber:@(integer)]; nslog(@"threeseparated: %@", threeseparated);
output: 4 273 095 782
Comments
Post a Comment