dimanche 26 juillet 2020

Swift - for loop statement not taking an absolute value

Such a weird question, but I set up this code in a playground:

let currentNumber = "1999999999"
    
    let absNumber = abs(Double(currentNumber)!)

var digitCount = 0

    if absNumber > 999999999 {
        
        for n in currentNumber {
            
            if n.isNumber {
                
                digitCount += 1
                
            } else {
                break
            }
            print(digitCount)
            
            
        }
        
        
        }

As written, this code gets evaluated and my for loop runs...however, if is set my string to "-1999999999", the for loop doesn't run. The absolute value of -1999999999 is 100% greater than 999999999, so what did I miss?

Aucun commentaire:

Enregistrer un commentaire