Skip to content

Instantly share code, notes, and snippets.

@demonar
Last active September 12, 2015 18:56
Show Gist options
  • Save demonar/d00750028bf26ee823ae to your computer and use it in GitHub Desktop.
Save demonar/d00750028bf26ee823ae to your computer and use it in GitHub Desktop.
import Foundation
extension Float {
func format(f: String) -> String {
return String(format: "%\(f)f", self)
}
mutating func roundTo(decimals: Int) {
let divisor = pow(10.0, Float(decimals))
self = round(self * divisor) / divisor
}
func roundTo(decimals: Int) -> Float {
let divisor = pow(10.0, Float(decimals))
return round(self * divisor) / divisor
}
func roundedUpString(decimals: Int) -> String {
let divisor = pow(10.0, Float(decimals))
var roundedNumber = "\(ceil(self * divisor) / divisor)"
if roundedNumber.hasSuffix(".0") {
roundedNumber.substringToIndex(find(roundedNumber, ".")!)
}
return roundedNumber
}
func descriptionWithLocale(locale: NSLocale) -> String? {
return NSNumber(float: self).descriptionWithLocale(locale)
}
}
extension Double {
func format(f: String) -> String {
return String(format: "%\(f)f", self)
}
mutating func roundTo(decimals: Int) {
let divisor = pow(10.0, Double(decimals))
self = round(self * divisor) / divisor
}
func roundTo(decimals: Int) -> Double {
let divisor = pow(10.0, Double(decimals))
return round(self * divisor) / divisor
}
func roundedUpString(decimals: Int) -> String {
let divisor = pow(10.0, Double(decimals))
var roundedNumber = "\(ceil(self * divisor) / divisor)"
if roundedNumber.hasSuffix(".0") {
roundedNumber.substringToIndex(find(roundedNumber, ".")!)
}
return roundedNumber
}
func descriptionWithLocale(locale: NSLocale) -> String? {
return NSNumber(double: self).descriptionWithLocale(locale)
}
}
extension Int {
func descriptionWithLocale(locale: NSLocale) -> String? {
return NSNumber(integer: self).descriptionWithLocale(locale)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment