将高数转换为低格式

我怎样才能改变高数字如下:

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) 

您可以将此function添加为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