Skip to content

Instantly share code, notes, and snippets.

@hossamghareeb
Created March 29, 2018 09:45
Show Gist options
  • Save hossamghareeb/10e8225b3135537d34a808027596d6ce to your computer and use it in GitHub Desktop.
Save hossamghareeb/10e8225b3135537d34a808027596d6ce to your computer and use it in GitHub Desktop.
Get digit at index in Int in Swift
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