[英]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. 也许这是一个愚蠢的答案,但我对ios很新。我必须在我的应用程序中以特定的方式显示一个数字。 Currently, I have numbers that I receive from a service in an NSString.
目前,我有从NSString中的服务收到的数字。 But, eg 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?
但是,例如,如果我有一个数字:123456789,我需要在标签中显示为123.456.789 ..任何人都知道是否有某种方法可以做到这一点?
Edit: add ios and iphone tags 编辑:添加ios和iphone标签
Use NSNumberFormatter
: 使用
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: 如果您的数字首先以字符串形式出现,那么您首先需要使用
NSString
的intValue
方法创建一个数字:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/intValue
[NSString localizedStringWithFormat:@"%@", myNumber];
This Worked for me: 这对我有用:
NSNumberFormatter *numFormatter = [NSNumberFormatter new];
[numFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numFormatter setUsesGroupingSeparator:YES];
[numFormatter setGroupingSeparator:@","];
[numFormatter setGroupingSize:3];
NSString * newString = [numFormatter stringFromNumber:[NSNumber numberWithDouble:43423423423242]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.