Last active
September 12, 2015 18:56
-
-
Save demonar/d00750028bf26ee823ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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