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

后端 未结 13 2096
陌清茗
陌清茗 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:08
    extension Int {
        var roundedWithAbbreviations: String {
            let number = Double(self)
            let thousand = number / 1000
            let million = number / 1000000
            if million >= 1.0 {
                return "\(round(million*10)/10)M"
            }
            else if thousand >= 1.0 {
                return "\(round(thousand*10)/10)K"
            }
            else {
                return "\(self)"
            }
        }
    }
    
    print(11.roundedWithAbbreviations)          // "11"
    print(11111.roundedWithAbbreviations)       // "11.1K"
    print(11111111.roundedWithAbbreviations)    // "11.1 M"
    
    0 讨论(0)
  • 2021-01-01 13:09

    For swift 4.0. its work completely fine and answer based on @user3483203

    Function for convert Double value to String

    func formatPoints(num: Double) ->String{
        var thousandNum = num/1000
        var millionNum = num/1000000
        if num >= 1000 && num < 1000000{
            if(floor(thousandNum) == thousandNum){
                return("\(Int(thousandNum))k")
            }
            return("\(thousandNum.roundToPlaces(places: 1))k")
        }
        if num > 1000000{
            if(floor(millionNum) == millionNum){
                return("\(Int(thousandNum))k")
            }
            return ("\(millionNum.roundToPlaces(places: 1))M")
        }
        else{
            if(floor(num) == num){
                return ("\(Int(num))")
            }
            return ("\(num)")
        }
    
    }
    

    Make one Double extension

    extension Double {
        /// Rounds the double to decimal places value
        mutating func roundToPlaces(places:Int) -> Double {
            let divisor = pow(10.0, Double(places))
            return Darwin.round(self * divisor) / divisor
        }
    }
    

    Usage of above function

    UILABEL.text = formatPoints(num: Double(310940)!)

    Output :

    0 讨论(0)
  • 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)
    }
    }
    
    0 讨论(0)
  • 2021-01-01 13:18

    Solution above (from @qlear) in Swift 3:

    func formatPoints(num: Double) -> String {
        var thousandNum = num / 1_000
        var millionNum = num / 1_000_000
        if  num >= 1_000 && num < 1_000_000 {
            if  floor(thousandNum) == thousandNum {
                return("\(Int(thousandNum))k")
            }
            return("\(thousandNum.roundToPlaces(1))k")
        }
        if  num > 1_000_000 {
            if  floor(millionNum) == millionNum {
                return "\(Int(thousandNum))k"
            }
            return "\(millionNum.roundToPlaces(1))M"
        }
        else{
            if  floor(num) == num {
                return "\(Int(num))"
            }
            return "\(num)"
        }
    }
    
    extension Double {
        // Rounds the double to decimal places value
        mutating func roundToPlaces(_ places : Int) -> Double {
            let divisor = pow(10.0, Double(places))
            return (self.rounded() * divisor) / divisor
        }
    }
    
    0 讨论(0)
  • 2021-01-01 13:18

    Minor improvement to @chrisz answer, Swift-4 Doble extension - This works fine in all cases.

    extension Double {
    
      // Formatting double value to k and M
      // 1000 = 1k
      // 1100 = 1.1k
      // 15000 = 15k
      // 115000 = 115k
      // 1000000 = 1m
      func formatPoints() -> String{
            let thousandNum = self/1000
            let millionNum = self/1000000
            if self >= 1000 && self < 1000000{
                if(floor(thousandNum) == thousandNum){
                    return ("\(Int(thousandNum))k").replacingOccurrences(of: ".0", with: "")
                }
                return("\(thousandNum.roundTo(places: 1))k").replacingOccurrences(of: ".0", with: "")
            }
            if self > 1000000{
                if(floor(millionNum) == millionNum){
                    return("\(Int(thousandNum))k").replacingOccurrences(of: ".0", with: "")
                }
                return ("\(millionNum.roundTo(places: 1))M").replacingOccurrences(of: ".0", with: "")
            }
            else{
                if(floor(self) == self){
                    return ("\(Int(self))")
                }
                return ("\(self)")
            }
        }
    
        /// Returns rounded value for passed places
        ///
        /// - parameter places: Pass number of digit for rounded value off after decimal
        ///
        /// - returns: Returns rounded value with passed places
        func roundTo(places:Int) -> Double {
            let divisor = pow(10.0, Double(places))
            return (self * divisor).rounded() / divisor
        }
    }
    




    0 讨论(0)
  • 2021-01-01 13:21

    Here is my approach to it.

    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 < 1000000 {
            return "\(self/1000)k"
        }
    
        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)
    }
    

    Below are some examples:

    print(913.shorted())
    print(1001.shorted())
    print(1699.shorted())
    print(8900.shorted())
    print(10500.shorted())
    print(17500.shorted())
    print(863500.shorted())
    print(1200000.shorted())
    print(3010000.shorted())
    print(11800000.shorted())
    
    913
    1K
    1.6K
    8.9K
    10k
    17k
    863k
    1.2M
    3M
    11M
    
    0 讨论(0)
提交回复
热议问题