Swift 2.0 Format 1000's into a friendly K's

后端 未结 13 2184
陌清茗
陌清茗 2021-01-01 12:51

I\'m trying to write a function to present thousands and millions into K\'s and M\'s For instance:

1000 = 1k
1100 = 1.1k
15000 = 15k
115000 = 115k
1000000 =          


        
13条回答
  •  情话喂你
    2021-01-01 13:12

    if you want in lacs :

     extension Int {
    func shorted() -> String {
        if self >= 1000 && self < 10000 {
            return String(format: "%.1fK", Double(self/100)/10).replacingOccurrences(of: ".0", with: "")
        }
    
        if self >= 10000 && self < 100000 {
            return "\(self/1000)k"
        }
    
        if self >= 100000 && self < 1000000 {
            return String(format: "%.1fL", Double(self/10000)/10).replacingOccurrences(of: ".0", with: "")
        }
    
        if self >= 1000000 && self < 10000000 {
            return String(format: "%.1fM", Double(self/100000)/10).replacingOccurrences(of: ".0", with: "")
        }
    
        if self >= 10000000 {
            return "\(self/1000000)M"
        }
    
        return String(self)
    }
    }
    

提交回复
热议问题