Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

how to get the digit count of number swift

public extension Int {
/// returns number of digits in Int number
public var digitCount: Int {
    get {
        return numberOfDigits(in: self)
    }
}
/// returns number of useful digits in Int number
public var usefulDigitCount: Int {
    get {
        var count = 0
        for digitOrder in 0..<self.digitCount {
            /// get each order digit from self
            let digit = self % (Int(truncating: pow(10, digitOrder + 1) as NSDecimalNumber))
                / Int(truncating: pow(10, digitOrder) as NSDecimalNumber)
            if isUseful(digit) { count += 1 }
        }
        return count
    }
}
// private recursive method for counting digits
private func numberOfDigits(in number: Int) -> Int {
    if number < 10 && number >= 0 || number > -10 && number < 0 {
        return 1
    } else {
        return 1 + numberOfDigits(in: number/10)
    }
}
// returns true if digit is useful in respect to self
private func isUseful(_ digit: Int) -> Bool {
    return (digit != 0) && (self % digit == 0)
}
Comment

PREVIOUS NEXT
Code Example
Shell :: remove commit from PR 
Shell :: git deleted files update 
Shell :: docker machine keep restarting 
Shell :: mySQL root password config 
Shell :: bash substitution 
Shell :: install redux 
Shell :: sed replace with variable 
Shell :: connectivity_plus flutter 
Shell :: nano show line numbers 
Shell :: query in github api 
Shell :: babel source maps 
Shell :: docker gpio 
Shell :: linux grep regex return match 
Shell :: How to download torrents from the command-line on Linux 
Shell :: sudo-get update 
Shell :: if statement bash 
Shell :: git show whole file at commit 
Shell :: bash comment section to a file 
Shell :: create file from terminal using cat 
Shell :: piping commands 1 
Shell :: how to shutdown system immediately with shutdown command 
Shell :: git add an entire folder to commit 
Shell :: ssh file transfer 
Shell :: dracula theme gnome terminal 
Shell :: batch script comment 
Shell :: mac make bootable usb 
Shell :: how to remove apt repository ubuntu 
Shell :: redis dockerhub 
Shell :: shell pipe 
Shell :: java status archlinux 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =