Search code examples
swiftnumberstrim

Removing Digits from a Number


Does anybody know if there is a way of removing (trimming) the last two digits or first two digits from a number. I have the number 4131 and I want to separate that into 41 and 31, but after searching the Internet, I've only managed to find how to remove characters from a string, not a number. I tried converting my number to a string, then removing characters, and then converting it back to a number, but I keep receiving errors.

I believe I will be able to receive the first two digit by dividing the number by 100 and then rounding the number down, but I don't have an idea of how to get the last two digits?

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


Solution

  • Try this:

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

    The playground's output is what you need.

    Playground