Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNumber with commas

Maybe this is a silly answer but I'm pretty new on ios.. I have to show a number in an specific way in my app. Currently, I have numbers that I receive from a service in an NSString. But, e.g. if I have a number: 123456789, i need to show it in a label as 123.456.789 .. anyone knows if there is some way to do that?

Edit: add ios and iphone tags

like image 699
Yotes Avatar asked Mar 16 '26 06:03

Yotes


2 Answers

Use NSNumberFormatter:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *formattedOutput = [formatter stringFromNumber:[attributesDict objectForKey:NSFileSystemFreeSize]];
NSLog(@"System free space: %@", formattedOutput);

If your number comes in as a string first, you'll need to make a number out of it first which you can do using NSString's intValue method:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue

like image 185
Almo Avatar answered Mar 17 '26 22:03

Almo


[NSString localizedStringWithFormat:@"%@", myNumber];
like image 35
mxcl Avatar answered Mar 17 '26 21:03

mxcl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!