Removing digits from a number

Does anyone know if there is a way to remove (trim) the last two digits or the first two digits from a number. I have number 4131 and I want to divide it into 41 and 31, but after searching the Internet, I managed to find a way to remove characters from the string, not the number. I tried converting my number to a string, then deleting the characters, and then converting them back to a number, but I keep getting errors.

I believe that I can get the first two digits by dividing the number by 100, and then rounding the number down, but I have no idea how to get the last two digits?

Does anyone know which function to use to achieve what I'm trying to do, or can someone point me in the right direction.

+4
source share
2

:

var num = 1234

var first = num/100
var last = num%100

- , .

Playground

+11

func getLatTwoDigits(number : Int) -> Int {
     return number%100; //4131%100 = 31
}
func getFirstTwoDigits(number : Int) -> Int {
    return number/100; //4131/100 = 41
}

, . , .

func printDigits(number : Int) {
    var num = number

    while num > 0 {
        var digit = num % 10 //get the last digit
        println("\(digit)")
        num = num / 10 //remove the last digit
    }
}
+4

All Articles