Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
Created August 30, 2016 16:28
Show Gist options
  • Select an option

  • Save maxcampolo/fd4094dcf78a079d5059c0f8b3462d32 to your computer and use it in GitHub Desktop.

Select an option

Save maxcampolo/fd4094dcf78a079d5059c0f8b3462d32 to your computer and use it in GitHub Desktop.

Revisions

  1. maxcampolo created this gist Aug 30, 2016.
    20 changes: 20 additions & 0 deletions Double+Suffix.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    extension Double {

    var suffixNumber: String {
    get {
    var num = self
    let sign = num < 0 ? "-" : ""

    num = fabs(num)
    if num < 1000.0 {
    return "\(sign)\(Int(num))"
    }

    let exp = Int(log10(num) / 3.0 )
    let units = ["K", "M", "G", "T", "P", "E"]
    let roundedNum = round(10 * num / pow(1000.0, Double(exp))) / 10
    return "\(sign)\(roundedNum)\(units[exp - 1])"
    }
    }

    }