Created
March 29, 2018 09:45
-
-
Save hossamghareeb/10e8225b3135537d34a808027596d6ce to your computer and use it in GitHub Desktop.
Get digit at index in Int in Swift
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 Decimal { | |
var intValue: Int { | |
return NSDecimalNumber(decimal: self).intValue | |
} | |
} | |
extension Int { | |
func digitAt(i: Int) -> Int { | |
let div = pow(10, i).intValue | |
let x = self / div | |
return x % 10 | |
} | |
} | |
123.digitAt(i: 0) // 3 | |
123.digitAt(i: 1) // 2 | |
123.digitAt(i: 2) // 1 | |
123.digitAt(i: 5) // 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment