[swift]数字の各桁数を求めます


数字の各桁数を求めます
https://travelbeeee.tistory.com/4
let randomInt = 123 
var randomArr = Array<Int>(repeating: 0, count: 3)
for i in 0...2 {
      let currentDigit: Double = Double(3 - i)
      let randomNum = (randomInt % Int(pow(10.0, currentDigit))) / Int(pow(10.0, currentDigit - 1))
      randomArr[i] = randomNum
}
// randomArr = [1, 2, 3]
数字が321の場合.
3 = (321 % 1000)/100
2 = (321 % 100)/10
1 = (321 % 10)
参考までに、数字の総数の桁数を求めるには、String(숫자).countが最も簡単な方法です.