我怎樣才能改變高號碼是這樣的:轉換高的數字更低格式
1,000 = 1K
1,250 = 1,2K
10,200 = 10,2K
102,000 = 102K
1,200,000 = 1,2M
或類似的東西?
這是我的設置數量:
textCell?.ll1.text = "\(String(thumb[indexPath.row].count))"
textCell?.ll2.text = "\(String(love[indexPath.row].count))"
我怎樣才能改變高號碼是這樣的:轉換高的數字更低格式
1,000 = 1K
1,250 = 1,2K
10,200 = 10,2K
102,000 = 102K
1,200,000 = 1,2M
或類似的東西?
這是我的設置數量:
textCell?.ll1.text = "\(String(thumb[indexPath.row].count))"
textCell?.ll2.text = "\(String(love[indexPath.row].count))"
let formatter = NSByteCountFormatter()
就是這樣;)
例子:
let oneFormattedNumber = formatter.stringFromByteCount(1025000000000)
let formattedList = [1_000, 1_250, 10_200, 102_000, 1_200_000].map(formatter.stringFromByteCount)
可以作爲擴展添加此功能到Int:
extension Int {
func shortLiteralDescription() -> String {
var factor = 0
let tokens = ["","K", "M", "G","T","P"] //If you think you will need to express more, add them here
var value = Double(self);
while (value > 1000) {
value /= 1000
factor++
}
return "\(value)\(tokens[factor])"
}
}
然後:
400200.shortLiteralDescription() //400.2K
4000.shortLiteralDescription() //4.0K
我怎麼使用它在這樣的功能:'textCell .ll1.text =「\(字符串(thumbEmoji [indexPath.row] .Count之間))「'? –
textCell?.ll1.text = thumbEmoji [indexPath.row] .count.shortLiteralDescription() –
看一看NSByteCountFormatter ...... –