Swift 2.0将1000格式化为友好的K&#39

时间:2016-04-02 17:58:06

标签: ios swift numbers swift2 format

我试图写一个功能,向K&M和M提供数千和数百万的功能 例如:

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

这是我到目前为止的地方:

func formatPoints(num: Int) -> String {
    let newNum = String(num / 1000)
    var newNumString = "\(num)"
    if num > 1000 && num < 1000000 {
        newNumString = "\(newNum)k"
    } else if num > 1000000 {
        newNumString = "\(newNum)m"
    }

    return newNumString
}

formatPoints(51100) // THIS RETURNS 51K instead of 51.1K

如何让这个功能起作用,我缺少什么?

14 个答案:

答案 0 :(得分:17)

func formatPoints(num: Double) ->String{
    let thousandNum = num/1000
    let millionNum = num/1000000
    if num >= 1000 && num < 1000000{
        if(floor(thousandNum) == thousandNum){
            return("\(Int(thousandNum))k")
        }
        return("\(thousandNum.roundToPlaces(1))k")
    }
    if num > 1000000{
        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
    func roundToPlaces(places:Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return round(self * divisor) / divisor
    }
}

如果数字是完整的,更新的代码现在不应返回.0。现在应该输出1k而不是1.0k。我只是检查了双倍和它的地板是否相同。

我在这个问题中找到了双重扩展: Rounding a double value to x number of decimal places in swift

答案 1 :(得分:9)

以下扩展名执行以下操作 -

  1. 将数字10456显示为10.5k,将10006显示为10k(不显示.0小数。)
  2. 将对数百万的确切做以上并格式化,即10.5M和10M
  3. 将以货币格式格式化数千到9999,即使用逗号9,999

    extension Double {
        var kmFormatted: String {
    
            if self >= 10000, self <= 999999 {
                return String(format: "%.1fK", locale: Locale.current,self/1000).replacingOccurrences(of: ".0", with: "")
            }
    
            if self > 999999 {
                return String(format: "%.1fM", locale: Locale.current,self/1000000).replacingOccurrences(of: ".0", with: "")
            }
    
            return String(format: "%.0f", locale: Locale.current,self)
        }
    }
    
  4. 用法:

    let num: Double = 1000001.00 //this should be a Double since the extension is on Double
    let millionStr = num.kmFormatted
    print(millionStr)
    

    打印1M

    这就是行动 -

    enter image description here

答案 2 :(得分:7)

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"

答案 3 :(得分:5)

答案中的一些变化(对于Int而言是正确的百万):

func formatPoints(num: Int) ->String{
    let thousandNum = num/1000
    let millionNum = num/1000000
    if num >= 1000 && num < 1000000{
        if(thousandNum == thousandNum){
            return("\(thousandNum)k")
        }
        return("\(thousandNum)k")
    }
    if num > 1000000{
        if(millionNum == millionNum){
            return("\(millionNum)M")
        }
        return ("\(millionNum)M")
    }
    else{
        if(num == num){
            return ("\(num)")
        }
        return ("\(num)")
    }

}

答案 4 :(得分:3)

Swift 3中的上述解决方案(来自@qlear):

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
    }
}

答案 5 :(得分:2)

要添加到答案中,这是Swift 4.X版本,使用循环可以在需要时轻松添加/删除单元:

extension Double {
    var shortStringRepresentation: String {
        if self.isNaN {
            return "NaN"
        }
        if self.isInfinite {
            return "\(self < 0.0 ? "-" : "+")Infinity"
        }
        let units = ["", "k", "M"]
        var interval = self
        var i = 0
        while i < units.count - 1 {
            if abs(interval) < 1000.0 {
                break
            }
            i += 1
            interval /= 1000.0
        }
        // + 2 to have one digit after the comma, + 1 to not have any.
        // Remove the * and the number of digits argument to display all the digits after the comma.
        return "\(String(format: "%0.*g", Int(log10(abs(interval))) + 2, interval))\(units[i])"
    }
}

示例:

$ [1.5, 15, 1000, 1470, 1000000, 1530000, 1791200000].map { $0.shortStringRepresentation }
[String] = 7 values {
  [0] = "1.5"
  [1] = "15"
  [2] = "1k"
  [3] = "1.5k"
  [4] = "1M"
  [5] = "1.5M"
  [6] = "1791.2M"
}

答案 6 :(得分:2)

对于 swift 4.0。,它的工作完全正常,并且可以根据 @ user3483203

进行回答。

将Double值转换为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)")
    }

}

设置一个双扩展名

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
    }
}

上述功能的使用

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

输出:

enter image description here

答案 7 :(得分:1)

基于@qlear的解决方案。
我注意到,如果数字正好是1000000,它将返回1000000未格式化。
所以我已将其添加到该功能中。我还包括负值..因为不是每个人都在赚钱!

func formatPoints(num: Double) ->String{
    let thousandNum = num/1000
    let millionNum = num/1000000
    if num > 0
    {
        if num >= 1000 && num < 1000000{
            if(floor(thousandNum) == thousandNum){
                return("\(Int(thousandNum))k")
            }
            return("\(round1(thousandNum, toNearest: 0.01))k")
        }
        if num > 1000000{
            if(floor(millionNum) == millionNum){
                return("\(Int(thousandNum))k")
            }
            return ("\(round1(millionNum, toNearest: 0.01))M")
        }
        else if num == 1000000
        {
            return ("\(round1(millionNum, toNearest: 0.01))M")
        }
        else{
            if(floor(num) == num){
                return ("\(Int(num))")
            }
            return ("\(round1(num, toNearest: 0.01))")
        }
    }
    else
    {

        if num <= -1000 && num > -1000000{
            if(floor(thousandNum) == thousandNum){
                return("\(Int(thousandNum))k")
            }
            return("\(round1(thousandNum, toNearest: 0.01))k")
        }
        if num < -1000000{
            if(floor(millionNum) == millionNum){
                return("\(Int(thousandNum))k")
            }
            return ("\(round1(millionNum, toNearest: 0.01))M")
        }
        else if num == -1000000
        {
            return ("\(round1(millionNum, toNearest: 0.01))M")
        }
        else{
            if(floor(num) == num){
                return ("\(Int(num))")
            }
            return ("\(round1(num, toNearest: 0.01))")
        }
    }

}

当然是数字延伸:

extension Double {
    /// Rounds the double to decimal places value
    func round1(_ value: Double, toNearest: Double) -> Double {
        return Darwin.round(value / toNearest) * toNearest
    }

}

答案 8 :(得分:0)

@chrisz 答案的轻微改进, Swift-4 Doble extension - 这在所有情况下均可正常使用。

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
    }
}

enter image description here
enter image description here
enter image description here
enter image description here

答案 9 :(得分:0)

这是我的解决方法。

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)
}

下面是一些示例:

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

答案 10 :(得分:0)

如果您想使用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)
}
}

答案 11 :(得分:0)

由于我们或多或少都不同意

func FormatFriendly(num: Double) ->String {
    var thousandNum = num/1000
    var millionNum = num/1000000

    if num >= 1000 && num < 1000000{
        if(floor(thousandNum) == thousandNum){
            return("\(Int(thousandNum))K").replacingOccurrences(of: ".0", with: "")
        }
        return("\(thousandNum.roundToPlaces(places: 1))K").replacingOccurrences(of: ".0", with: "")
    }

    if num >= 1000000{
        //if(floor(millionNum) == millionNum){
            //return("\(Int(thousandNum))K").replacingOccurrences(of: ".0", with: "")
        //}
    return ("\(millionNum.roundToPlaces(places: 1))M").replacingOccurrences(of: ".0", with: "")
    }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 Darwin.round(self * divisor) / divisor
    }
}

答案 12 :(得分:0)

我已经将@AnBisw的答案转换为使用switch(对构建时间友好):

   extension Double {
    var kmFormatted: String {
        switch self {
        case ..<1_000:
            return String(format: "%.0f", locale: Locale.current, self)
        case 1_000 ..< 999_999:
            return String(format: "%.1fK", locale: Locale.current, self / 1_000).replacingOccurrences(of: ".0", with: "")
        default:
            return String(format: "%.1fM", locale: Locale.current, self / 1_000_000).replacingOccurrences(of: ".0", with: "")
        }
    }
}

答案 13 :(得分:0)

此解决方案使用 ByteCountFormatter,但适用于任何数字类型的任何大数字缩写。为什么 Apple 为字节编写的这个格式化程序仍然未知。

extension Numeric {
    
    var abbreviated: String {
        let bytesString = ByteCountFormatter.string(fromByteCount: (self as! NSNumber).int64Value, countStyle: .decimal)
        let numericString = bytesString
            .replacingOccurrences(of: "bytes", with: "")
            .replacingOccurrences(of: "B", with: "") // removes B (bytes) in 'KB'/'MB'/'GB'
            .replacingOccurrences(of: "G", with: "B") // replace G (Giga) to just B (billions)
        return numericString.replacingOccurrences(of: " ", with: "")
    }
}