Created
October 18, 2024 19:25
-
-
Save loganblevins/bf3de415246fc4a4a6bf91733b3b9956 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
extension Double { | |
func safeInt() -> Int? { | |
guard isFinite else { return nil } | |
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
like: https://stackoverflow.com/a/40544010 with addition of
isFinite
(which checks for NaN and infinity)