Created
October 16, 2019 14:50
-
-
Save mudasir093/c241d024ffe57128e64bcf5b0f5d45f7 to your computer and use it in GitHub Desktop.
Double and Decimal Extensions
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
#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