Get nth integer digits in Swift?

Let's say I have a value representing 1927. Now I want to get the last 2 digits of the specified year. Is there an efficient way to do this in Swift? So far, I have figured out the following method based on my SO study:

// My goal is to start with 1927 and end with 27

let fullYear = 1927 as Float  // --> 1927 
let valueToSubtract = Int(fullYear/100)  // --> 19
let splitNumber = fullYear/100 as NSNumber  // --> 19.27
let decimalValue = Float(splitNumber) - Float(valueToSubtract)  // -->0.2700005
let finalNumber = Double(round(1000 * decimalValue)/10)  // --> 27

It seems too cumbersome. I'm new to programming and Swift, have I missed an easier way?

+4
source share
1 answer

The last two digits - use the modulo operator: 1927 % 100gives 27.

+14
source

All Articles