NSByteCountFormatter
可以幫助:
int number = 100000;
NSString *binaryFormatted = [NSByteCountFormatter stringFromByteCount:number countStyle:NSByteCountFormatterCountStyleDecimal];
NSLog(@"binaryFormatted: %@", binaryFormatted);
NSString *digits = [binaryFormatted substringToIndex:binaryFormatted.length-3];
NSString *units = [binaryFormatted substringWithRange:NSMakeRange(binaryFormatted.length-2, 1)];
NSString *currencyFormatted = [NSString stringWithFormat:@"$%@%@", digits, units];
NSLog(@"currencyFormatted: %@", currencyFormatted);
的NSLog輸出
binaryFormatted:100 KB
currencyFormatted:$ 100K
這是很容易設置貨幣方面。但要根據數字添加您的k和M後綴,您需要子類「NSNumberFormatter」 –