2014-07-16 56 views
0

我需要創建一個採用整數值並將它們發送到格式化貨幣的NSFormatter,但捕獲的是它必須爲數千和數百萬加上K和M。 E.G:用於轉換爲貨幣的NSNumberFormatter

130000 -> $130k 
12000 -> $12k 
10000000 ->$10M 

它必須是一個數據類型NSNumberFormatter,因爲我將使用它來設置這需要參數NSNumberFormatter(我格式化圖表的軸標籤)的屬性。

+0

這是很容易設置貨幣方面。但要根據數字添加您的k和M後綴,您需要子類「NSNumberFormatter」 –

回答

0

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