Skip to content

Instantly share code, notes, and snippets.

@mudasir093
Created October 16, 2019 14:50
Show Gist options
  • Save mudasir093/c241d024ffe57128e64bcf5b0f5d45f7 to your computer and use it in GitHub Desktop.
Save mudasir093/c241d024ffe57128e64bcf5b0f5d45f7 to your computer and use it in GitHub Desktop.
Double and Decimal Extensions
#Useful Decimal Extension
extension Decimal {
//#Extension for Decimal to Double Conversion
var doubleValue:Double {
return NSDecimalNumber(decimal:self).doubleValue
}
}
//#Useful Double Extension
extension Double {
//#Extension For round Double till specific decimal place
func rounded(toPlaces places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
// #Extension For Double to Integer (Int)
func toInt() -> Int? {
if self >= Double(Int.min) && self < Double(Int.max) {
return Int(self)
} else {
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment