NSNumberFormatter : Show 'k' instead of ',000' in large numbers?

后端 未结 3 1323
我寻月下人不归
我寻月下人不归 2021-01-05 02:38

I\'d like to change my large numbers from 100,000 to $100K if this is possible.

This is what I have so far:

let valueFormatter = NSN         


        
3条回答
  •  抹茶落季
    2021-01-05 02:59

    I think you can add an extension to NSNumberFormatter. Try the following, I didn't test it so let me know in the comment if it needs to be edited

    extension NSNumberFormatter {
        func dividedByK(number: Int)->String{
            if (number % 1000) == 0 {
                let numberK = Int(number / 1000)
                return "\(numberK)K"
            }
            return "\(number)"
        }
    }
    

提交回复
热议问题